Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算嵌套列表中团队的获胜次数_Python_List_Count_Nested - Fatal编程技术网

Python 计算嵌套列表中团队的获胜次数

Python 计算嵌套列表中团队的获胜次数,python,list,count,nested,Python,List,Count,Nested,我已经编写了一些代码,我正试图使用这些代码来计算一个足球队赢得一场比赛的次数。比赛被放入一个嵌套列表中,其中每个子列表分别包含两支球队的名字和他们在比赛中的得分 L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', 2'], ['Giants', 'Steelers', '3', '5']] 然而,这份名单要大得多,包含了更多参加比赛的足球队 我已经有了一份最终的名单,其中包含了每支球队的名字,以及他们踢过的比

我已经编写了一些代码,我正试图使用这些代码来计算一个足球队赢得一场比赛的次数。比赛被放入一个嵌套列表中,其中每个子列表分别包含两支球队的名字和他们在比赛中的得分

L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', 2'], ['Giants', 'Steelers', '3', '5']]
然而,这份名单要大得多,包含了更多参加比赛的足球队

我已经有了一份最终的名单,其中包含了每支球队的名字,以及他们踢过的比赛的数量,我已经成功地计算了

finalList = [['Patriots', 7], ['Giants', 3], ['Steelers', 8]]
我希望输出如下:

finalList = [['Patriots', 7, 2], ['Giants', 3, 0], ['Steelers', 8, 1]]
[['Giants', 5, 1, 0, 1]]
因为爱国者队打了7场比赛,赢了2场,巨人队打了3场比赛,赢了0场,钢人队打了8场比赛,赢了1场

这是到目前为止我的代码,它没有为某些匹配提供正确的结果。它也不会对计数求和,因此只会附加1和0,如下所示:

finalList = [['Patriots', 7, 2], ['Giants', 3, 0], ['Steelers', 8, 1]]
[['Giants', 5, 1, 0, 1]]
我的代码:

for i in L:
    countLeft = 0
    countRight = 0
    if i[2]>i[3]:
        countLeft += 1
    elif i[3]>i[2]:
        countRight += 1
        for k in finalList:
            if i[0]==k[0]:
                k.append(countLeft)
            elif i[1]==k[0]:
                k.append(countRight)
print(finalList)
我也不允许在我的代码中使用任何字典

尝试以下操作:

for k in finalList:
    k.append(0)

for i in L:
    if int(i[2]) > int(i[3]):
            for k in finalList:
                    if k[0] == i[0]:
                            k[2]+=1
    elif int(i[3]) > int(i[2]):
            for k in finalList:
                    if k[0] == i[1]:
                            k[2]+=1

请尝试以下操作:

for k in finalList:
    k.append(0)

for i in L:
    if int(i[2]) > int(i[3]):
            for k in finalList:
                    if k[0] == i[0]:
                            k[2]+=1
    elif int(i[3]) > int(i[2]):
            for k in finalList:
                    if k[0] == i[1]:
                            k[2]+=1


您可以使用
集合
模块中的
计数器
,并使用
列表理解
获得所需的结果,如以下示例所示:

from collections import Counter

a = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]
b = [['Patriots', 7], ['Giants', 3], ['Steelers', 8]]

wins = Counter(team1 if int(team1_s) > int(team2_s) else team2 if int(team2_s) > int(team1_s) else None for team1, team2, team1_s, team2_s in a)

final = final = [[k,l,c[k]] if k in wins else [k,l,0] for k,l in b]

print(final)
输出:

[['Patriots', 7, 2], ['Giants', 3, 0], ['Steelers', 8, 1]]

您可以使用
集合
模块中的
计数器
,并使用
列表理解
获得所需的结果,如以下示例所示:

from collections import Counter

a = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]
b = [['Patriots', 7], ['Giants', 3], ['Steelers', 8]]

wins = Counter(team1 if int(team1_s) > int(team2_s) else team2 if int(team2_s) > int(team1_s) else None for team1, team2, team1_s, team2_s in a)

final = final = [[k,l,c[k]] if k in wins else [k,l,0] for k,l in b]

print(final)
输出:

[['Patriots', 7, 2], ['Giants', 3, 0], ['Steelers', 8, 1]]

对这很有效。我可以问一下,如果len(k)<3:@BillyWhales,为什么要加入
,这是为了防止调用
k[2]
时出现索引器;基本上,如果团队之前没有赢过,那么数组将只有2个索引长(
['Patriots',7]
),因此
k[2]
将引发一个错误。我如何编辑它,以便如果团队赢了0场比赛,它将只追加0场?太棒了!非常感谢你!对这很有效。我可以问一下,如果len(k)<3:
@BillyWhales,为什么要加入
,这是为了防止调用
k[2]
时出现索引器;基本上,如果团队之前没有赢过,那么数组将只有2个索引长(
['Patriots',7]
),因此
k[2]
将引发一个错误。我如何编辑它,以便如果团队赢了0场比赛,它将只追加0场?太棒了!非常感谢你!