Reputation: 747
I'm working on a Baseball project and I'm trying to match names of athletes from two different csv files with their corresponding stats
For example I have two list of lists that look like this:
player_stats1 = [[Clayton Kershaw, stats, more stats, more stats]]
player_stats2 = [[Clayton Kershaw, stats, more stats, more stats]]
I have an if statement that checks if the two names are the same
if player_stats1[1][1] and player_stats1[1][2] == player_stats2[1][1] and player_stats2[1][2]:
print('True')
else:
print('false')
I need to check if the names are the same and if they are append the data from players_stats2[1][0] and players_stats2[1][5] to the original list, players_stats1
I've tried
length1 = len(player_stats1)
players_stats3 = []
i = 1
while i < length1:
if player_stats1[1][i] and player_stats1[1][i + 1] == player_stats2[1][i] and player_stats2[1][i + 1]:
players_stats3.append(player_stats2[i][0], player_stats2[i][5])
else:
print('')
i += 1
but I keep getting an error.
IndexError: list index out of range
Upvotes: 0
Views: 57
Reputation: 5384
You have a list of lists. So change how you are indexing.
# get the i-th list of player stats | [Clayton Kershaw, stats, more stats, more stats]
player_stats1[i]
# get the name in the i-th list of player stats
player_stats1[i][1]
In the example you have the player name as the first item so if that is correct, then it should be the following to get the name.
player_stats1[i][0]
Example
>>> lsts = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
>>> lsts[0][1] # first list, second item
2
There is also a problem with your original if statement.
if player_stats1[1][1] and player_stats1[1][2] == player_stats2[1][1] and player_stats2[1][2]:
print('True')
else:
print('false')
This will always print True if both your list contains "truthy" values. You instead need to compare both.
player_stats1[1][1] == player_stats2[1][1] and player_stats1[1][2] == player_stats2[1][2]
Alternatives:
You could also use zip
to group your lists together and iterate through both of them at the same time.
player_stats1 = [ ["A", "B"], ["A", "C"], ["D", "A"], ["E", "E"]]
player_stats2 = [ ["A", "A"], ["D", "A"], ["A", "C"], ["E", "E"]]
res = []
for p1, p2 in zip(player_stats1, player_stats2):
if p1[0] == p2[0] and p1[1] == p2[1]:
res.append( (p2[0], p2[1]) )
print(res)
The downside to zip
is that:
If you want to find all matches then you will need to loop through both comparing each of them.
res = []
for p2 in player_stats1:
for p1 in player_stats2:
if p1[0] == p2[0] and p1[1] == p2[1]:
res.append( (p2[0], p2[1]) )
print(res)
Upvotes: 1
Reputation: 877
You have
length1 = len(player_stats1)
But then use:
player_stats1[1][i]
How do you know player_stats1[1][i] exists?
Shouldn't you have:
length1 = len(player_stats1[1])
Additional:
length1 = len(player_stats1)
players_stats3 = []
i = 1
while i < length1:
if player_stats1[i][1] and player_stats1[i][2] == player_stats2[i][1] and player_stats2[i][2]:
players_stats3.append(player_stats2[i][0], player_stats2[i][5])
else:
print('')
i += 1
This is assuming player_stats1 and player_stats2 are of the same length. Generally, though, it seems like the wrong solution. You should create a key/value pair out of one csv, and check against the other, because in this solution, if you have a matching player in the wrong order, you wouldn't find it.
Upvotes: 0
Reputation: 61
change the while i < length1 -1
: because the length would be 4 but you only have list indexing up to [3]
Upvotes: 0