Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_Tuples - Fatal编程技术网

Python 如何在发现重复的元组时更新元组

Python 如何在发现重复的元组时更新元组,python,list,tuples,Python,List,Tuples,我有一个元组,它由我循环并存储的许多团队组成。对我来说,下一步是找到重复项并只存储一个团队,但是更新数字,该数字指示有多少人与该团队相关 _teamList = [] for obj in context['object_list']: name = obj.team.name number = 1 _teamList.append((name, number)) 输入的示例如下所示: [("Team bobcat", 1), ("Tea

我有一个元组,它由我循环并存储的许多团队组成。对我来说,下一步是找到重复项并只存储一个团队,但是更新数字,该数字指示有多少人与该团队相关

_teamList = []
    for obj in context['object_list']:
        name = obj.team.name
        number = 1
        _teamList.append((name, number))
输入的示例如下所示:

[("Team bobcat", 1), ("Team Coffe", 1)]
下面是获取团队并向其中添加一个团队的代码

我试过这样的方法:

seen = set()
    uniq = []
    for x in _teamList:
        if x not in seen:
            x = 1 + x[1]
            uniq.append(x)
            seen.add(x)

有人能给我一些提示吗?

你可以使用集合中的“计数器”。

这将自动为您分组相同的名称。您不需要计算出现的次数

例如:

>>> from collections import Counter as c
>>> a = ('team a', 'team b', 'team a', 'team c', 'team b')
>>> c(a)
Counter({'team a': 2, 'team b': 2, 'team c': 1})

您可以使用集合中的“计数器”。

这将自动为您分组相同的名称。您不需要计算出现的次数

例如:

>>> from collections import Counter as c
>>> a = ('team a', 'team b', 'team a', 'team c', 'team b')
>>> c(a)
Counter({'team a': 2, 'team b': 2, 'team c': 1})

下面是一个基本Python解决方案:

a = ('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b')
d = {}

for x in a:
    if x in d.keys():
        d[x] += 1 
    else: 
        d[x] = 1

d
# {'team a': 2, 'team b': 4, 'team c': 1}
x=('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b') l = {} for i in x: if i not in l: l[i] = 1 else: l[i] = l[i] + 1 data = list(tuple(l.items())) print(data) #output as: [('team a', 2), ('team b', 4), ('team c', 1)] 如果希望输出为元组,请添加:

tuple((name, ct) for name, ct in d.items())
# (('team a', 2), ('team b', 4), ('team c', 1))

下面是一个基本Python解决方案:

a = ('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b')
d = {}

for x in a:
    if x in d.keys():
        d[x] += 1 
    else: 
        d[x] = 1

d
# {'team a': 2, 'team b': 4, 'team c': 1}
x=('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b') l = {} for i in x: if i not in l: l[i] = 1 else: l[i] = l[i] + 1 data = list(tuple(l.items())) print(data) #output as: [('team a', 2), ('team b', 4), ('team c', 1)] 如果希望输出为元组,请添加:

tuple((name, ct) for name, ct in d.items())
# (('team a', 2), ('team b', 4), ('team c', 1))

您可以参考此解决方案:

a = ('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b')
d = {}

for x in a:
    if x in d.keys():
        d[x] += 1 
    else: 
        d[x] = 1

d
# {'team a': 2, 'team b': 4, 'team c': 1}
x=('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b') l = {} for i in x: if i not in l: l[i] = 1 else: l[i] = l[i] + 1 data = list(tuple(l.items())) print(data) #output as: [('team a', 2), ('team b', 4), ('team c', 1)] x=(‘a队’、‘b队’、‘a队’、‘c队’、‘b队’、‘b队’、‘b队’) l={} 对于x中的i: 如果我不在l: l[i]=1 其他: l[i]=l[i]+1 数据=列表(元组(l.items()) 打印(数据) #输出为:[('team a',2),('team b',4),('team c',1)]
您可以参考此解决方案:

a = ('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b')
d = {}

for x in a:
    if x in d.keys():
        d[x] += 1 
    else: 
        d[x] = 1

d
# {'team a': 2, 'team b': 4, 'team c': 1}
x=('team a', 'team b', 'team a', 'team c', 'team b', 'team b', 'team b') l = {} for i in x: if i not in l: l[i] = 1 else: l[i] = l[i] + 1 data = list(tuple(l.items())) print(data) #output as: [('team a', 2), ('team b', 4), ('team c', 1)] x=(‘a队’、‘b队’、‘a队’、‘c队’、‘b队’、‘b队’、‘b队’) l={} 对于x中的i: 如果我不在l: l[i]=1 其他: l[i]=l[i]+1 数据=列表(元组(l.items()) 打印(数据) #输出为:[('team a',2),('team b',4),('team c',1)]
可能是@MikeScotty的复制品哦,好的。我会读到《米凯斯科蒂》的复制品哦,好的。威尔读那条线谢谢,这正是我需要的谢谢,这正是我需要的