Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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/8/lua/3.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 - Fatal编程技术网

Python 如何计算元组列表并将变量的频率导出到字典中?

Python 如何计算元组列表并将变量的频率导出到字典中?,python,Python,我一直在研究一个赋值的解决方案,其中我们接受一个元组对象列表,并返回一个包含列表中出现的所有字符串频率的字典 所以我一直在尝试使用集合中的计数器来计算元组列表中出现的键的频率 tuple_list = [('a',5), ('a',5), ('b',6), ('b',4), ('b',3), ('b',7)] 我无法让计数器只检查“a”或“b”或列表中的字符串 from collections import Counter def get_frequency(tuple_list):

我一直在研究一个赋值的解决方案,其中我们接受一个元组对象列表,并返回一个包含列表中出现的所有字符串频率的字典

所以我一直在尝试使用集合中的计数器来计算元组列表中出现的键的频率

tuple_list = [('a',5), ('a',5), ('b',6), ('b',4), ('b',3), ('b',7)]
我无法让计数器只检查“a”或“b”或列表中的字符串

from collections import Counter
def get_frequency(tuple_list): 
     C = Counter(new_list)
     print (C('a'), C('b')) 

 tuple_list = [('a',5), ('a',5), ('b',6), ('b',4), ('b',3), ('b',7)]
 freq_dict = get_frequency(tuple_list)
 for key in sorted(freq_dict.keys()):
    print("{}: {}".format(key, freq_dict[key]))
我期望的输出应该是
a:2b:4
,但我一直得到
a:0b:0

,因为每个元组中的第二个(数字)元素似乎是不相关的,所以您需要传递一系列您试图计数的字母。尝试列表理解:

>>> tuple_list = [('a',5), ('a',5), ('b',6), ('b',4), ('b',3), ('b',7)]
>>>
>>> items = [item[0] for item in tuple_list]
>>> items
['a', 'a', 'b', 'b', 'b', 'b']

>>> from collections import Counter
>>> c = Counter(items)
>>> print(c)
Counter({'b': 4, 'a': 2})

如果你不想使用计数器,你可以这样做列表的长度

unique_values = list(set([x[0] for x in tuple_list]))
a_dict = {}
for v in unique_values:
    a_dict[v] = len([x[1] for x in tuple_list if x[0] == v])

print(a_dict)
这给了你:

{'b': 4, 'a': 2}

因为您只想计算每个元组中的第一个元素(字符串),所以您应该只在第一个元素上使用counter对象,如下面的get_frequency函数所示:

def get_frequency(tuple_list):
    cnt = Counter()
    for tuple_elem in tuple_list:
        cnt[tuple_elem[0]] += 1
    return cnt

tuple_list = [('a',5), ('a',5), ('b',6)]
freq_dict = get_frequency(tuple_list)

for key, value in freq_dict.items():
    print(f'{key}: {value}')
另外,确保如果希望从函数接收值,通常需要使用return语句返回值


希望有帮助

另一种解决方案是使用
zip
next
将每个元组的第一项提取到一个新元组中,并将其输入到
计数器中

from collections import Counter
result = Counter(next(zip(*items)))

您不必使用计数器
+=1
。尝试
cnt.update(tuple_elem[0])
no,只是不习惯在使用
collections.Counter时看到这一点。我从文档()中获得了它,但是是的。。我想这两种方法都管用!