无法将两个列表加入正确的位置

时间:2017-04-28 12:29:04

标签: python list join

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

无论如何,我可以让它为列表中的每个团队输出这样的内容:

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

我试过了:

Zip(info,score)

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

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

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

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

如果有帮助,我还将文件上传到微小上传: link

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

看起来问题可以通过处理数据文件一次,逐行,计算新结果并将其添加到新列表来简化问题。

例如:

alert("Row index is: " + x.parentNode.parentNode.rowIndex);

答案 2 :(得分:0)

你可以做到

import csv
with open('premierleague.csv') as f:
    f.next()
    reader = csv.DictReader(f)
    for i in reader:
        data = ",".join([i['Team'], i['Matches Won'], i['Matches Drawn'], i['Matches Lost'], str(int(i['Matches Won'])*3)])
        print data