简体   繁体   中英

Appending values from one list to another

I have two csv files that I'm importing. There are values from one csv that I'm missing that I need to combine with another.

The first list looks like this stats =

[ ['0', 'Dallas', 'Keuchel,', 'HOU', '8', '8', '6', '0', '1.000', '1', '0', '7', '0', '58.2', '37', '12', '11', '6', '14', '50', '1.69'],

    ['1', 'Ervin', 'Santana,', 'MIN', '7', '7', '5', '1', '.833', '1', '1', '6', '0', '47.0', '21', '9', '9', '6', '16', '37', '1.72'],

    ['2', 'Jason', 'Vargas,', 'KC', '7', '7', '5', '1', '.833', '0', '0', '6', '0', '44.2', '33', '6', '5', '1', '8', '39', '1.01']]

The second looks like this salaries =

[['19249-12477', 'P', 'Chris', 'Chris Sale', 'Sale', '54.71', '7', '11800', 'TAM@BOS', 'BOS', 'TAM', '', '', '', ''],

['19249-5481', 'P', 'Max', 'Max Scherzer', 'Scherzer', '48.29', '7', '11700', 'PHI@WAS', 'WAS', 'PHI', '', '', '', ''],

['19249-6311', 'P', 'Madison', 'Madison Bumgarner', 'Bumgarner', '38.50', '4', '11000', 'CIN@SFG', 'SFG', 'CIN', '', 'Shoulder', '', '']]

Both list are different lengths. I need to pull salaries[i][1] and salaries[i][7] from list two and combine it with list one.

Here's the catch, I have to iterate through both list and make sure the names match or else I'll get the wrong values combined with the wrong players.

Here is my code so far

for name1, name2 in zip(salaries, stats):
    if name1[2] == name2[1] and name1[4] == name2[2]:
        stats.append(salaries[1][1])



for name1, name2 in zip(salaries, stats):
    if name1[2] == name2[1] and name1[4] == name2[2]:
        stats.append(salaries[1][7])

But when I run this it only appends once giving me the header value ['Position'] instead of iterating through the entire list and appending the values.

When I get this running, I also need to append the values to stats . Will I need to give it the specific index where it should be appended?

You can't simply zip the lists: the entries don't correspond by position. Instead, read in both lists, sort them by name, (see the sorting tutorial ) and then ...

Do you know that the two files include exactly the same players? If so, you can zip the lists at this point. If not, forget zip entirely. Instead, iterate through stats . For each player, find the name in salaries . Grab the two fields and insert them into that row of stats . If you're worried about the order, then yes, you have to specify where to put them -- use insert , not append .

That's the easy way. If you're worried about execution time (say, for all players in the history of professional sports), then there are faster ways to do the updates. For only 1500 players, don't worry about it: read in both lists, do the N-squared update, and write out the new file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM