gpt4 book ai didi

python - 无法在正确位置连接两个列表

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:43 24 4
gpt4 key购买 nike

所以我有这个功能,我想打开一个包含运动队数据的 csv 文件。从那里我希望它能够计算球队的总积分,因为我只知道球队名称、参加的比赛、胜利、平局和损失。我已经计算了分数,但现在无法将它们添加回列表中的正确位置。

我是否可以让它为列表中的每个团队输出类似的内容:

'ManchesterUnited,20,15,5,5,50'

我已经尝试过:

Zip(info,score)

[info + score for info, score in zip(info, score)]

但它不起作用,因为分数列表是整数列表而不是字符串。

有没有办法可以将此列表存储在变量中以便在以后的函数中使用?

编辑:这是 csv 文件的图像: csvFile

如果有帮助的话,我还将文件上传到tiny upload: link

最佳答案

这是一个演示,可能会有所帮助:

    >>> 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 文件并获取 scoreinfo 后,以下方法将起作用:

>>> 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']

关于python - 无法在正确位置连接两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com