Reputation: 3
I have a list containing 640 items (all floats). I have a list of tuples (two items in the tuple) containing 640 items. Both lists are ordered and line up with each other. I want to take the two items from the tuple and put them at the top of their corresponding list of floats. For example, both of these are at index[0] of their corresponding lists.
[[37.0,
40.1,
'27.2',
'58.3',
'.467',
'20.9',
'40.2',
'.519',
'6.3',
'18.1',
'.349',
'13.4',
'19.2',
'.698',
'12.4',
'19.5',
'31.9',
'15.2',
'9.1',
'6.9',
'10.5',
'15.5',
'74.1']
('syracuse', 2012)
I want it to be one list ['syracuse', 2012, 37.0, 40.1, 27.2, etc] and do that to all 640 items.
Here's what I've tried so far.
empty_list = []
team_year = [list(x) for x in team_year]
for i,v in enumerate (cbb_team_sites):
y = pd.read_html(v)[1]
y = list((y.loc[0].T)[1:]) --> this is what produces the list of floats
empty_list.append(y)
combined = list(zip(team_year,empty_list))
I've also tried:
`
for x in cbb_team_sites:
y = pd.read_html(x)[1]
y = list((y.iloc[0]).T)[1:]
team_stats_list.append(y)
for x in (range(640)):
team_stats_list.append(team_year[x])
`
I'm sure there's an easy way to do this and I'm just missing it. But after about 4+ hours of trying, I figure it's time to ask for help.
Thank you
Upvotes: 0
Views: 42
Reputation: 192
You didn't ask a specific question, but based on your comment ("I'm sure there's an easy way to do this and I'm just missing it."), I infer that you are asking for an easier solution or help debugging your code. I offer you the former.
# simplified example of data
a = [('syracuse', 2012), ('buffalo', 2022)]
b = [[1., 1.], [55., 55.]]
c = [[item[0], item[1]] for item in a]
[c[i] + b[i] for i in range(2)]
Output
> [['syracuse', 2012, 1.0, 1.0], ['buffalo', 2022, 55.0, 55.0]]
Upvotes: 0
Reputation: 10959
Replacing the last line in your first try with
combined = [list(t) + e for t, e in zip(team_year,empty_list)]
should work
Upvotes: 0
Reputation: 217
The zip()
function can help you.
The zip
built-in function mixes the elements of several iterables, combining those with the same index into a single group.
a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
b = (1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
print(tuple(zip(a, b)))
# ((1, 1.1), (2, 2.2), (3, 3.3), (4, 4.4), (5, 5.5), (6, 6.6), (7, 7.7), (8, 8.8), (9, 9.9), (10, 10.1))
Upvotes: 1