所以我有这个功能,我想打开一个包含运动队数据的 csv 文件。从那里我想计算球队的总分,因为我只给出了球队名称,比赛,胜利,平局损失。我已经计算了分数,但现在无法将它们添加回列表中的正确位置。
无论如何我可以让它为列表中的每个团队输出这样的东西:
'ManchesterUnited,20,15,5,5,50'
我试过了:
Zip(info,score)
[info + score for info, score in zip(info, score)]
但它不会工作,因为分数列表是整数而不是字符串的列表。
有没有办法可以将此列表存储在变量中以供以后的函数使用?
编辑:这是 csv 文件的图像:csvFile
如果有帮助,我还将文件上传到小上传:链接
这是一个演示,它可能会有所帮助:
>>> x = ['ManchesterUnited', "India"]
>>> y = [[20,15,5,5,50], [4,5,6,7,8]]
>>> [", ".join(s) for s in list(zip(x, [", ".join(map(str,t)) for t in y]))]
['ManchesterUnited, 20, 15, 5, 5, 50', 'India, 4, 5, 6, 7, 8']
@Nvision 对于您的情况,在查看csv
文件然后获取score
and 后info
,以下方法将起作用:
>>> score = [56, 50, 27, 49, 56, 33, 33, 25, 47, 55, 31, 36, 40, 27, 31, 66, 24, 30, 19, 22]
>>> info = ['Manchester City,27,17,5,5', 'Arsenal,26,15,5,6', 'Leicester City,27,7,6,14', 'Manchester United,26,13,10,3', 'Tottenham Hotspur,27,16,8,3', 'West Ham United,28,9,6,13', 'Southampton,26,9,6,11', 'Crystal Palace,27,7,4,16', 'Everton,28,13,8,7', 'Liverpool,28,16,7,5', 'Watford,27,8,7,12', 'Stoke City,28,9,9,10', 'West Bromwich Albion,28,11,7,10', 'Swansea City,28,8,3,17', 'Burnley,28,9,4,15', 'Chelsea,27,21,3,3', 'Hull City,28,6,6,16', 'Bournemouth,28,8,6,14', 'Sunderland,27,5,4,18', 'Middlesborough,27,4,10,13']
>>> result = [i + ',' + str(j) for i, j in zip(info, score)]
>>> result
['Manchester City,27,17,5,5,56', 'Arsenal,26,15,5,6,50', 'Leicester City,27,7,6,14,27', 'Manchester United,26,13,10,3,49', 'Tottenham Hotspur,27,16,8,3,56', 'West Ham United,28,9,6,13,33', 'Southampton,26,9,6,11,33', 'Crystal Palace,27,7,4,16,25', 'Everton,28,13,8,7,47', 'Liverpool,28,16,7,5,55', 'Watford,27,8,7,12,31', 'Stoke City,28,9,9,10,36', 'West Bromwich Albion,28,11,7,10,40', 'Swansea City,28,8,3,17,27', 'Burnley,28,9,4,15,31', 'Chelsea,27,21,3,3,66', 'Hull City,28,6,6,16,24', 'Bournemouth,28,8,6,14,30', 'Sunderland,27,5,4,18,19', 'Middlesborough,27,4,10,13,22']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句