Python:创建一个元组列表,其中包含列表中的值和出现次数
Python: create a list of tuple with value and number of occurence from list
基于列表,我需要创建一个元组列表,每个元组包含(值,nbr_of_occurence_of_the_value_in_the_list)。
我的代码可以正常工作,但我觉得它可以改进,有人知道如何改进这段代码吗?
def get_tuple_count_list(_list):
tuple_count_list = []
for v in _list:
if v not in [v1 for (v1,count) in tuple_count_list]:
tuple_count_list.append((v,1))
continue
i = [v1 for (v1,count) in tuple_count_list].index(v)
tuple_count_list[i] = (v, tuple_count_list[i][1]+1)
return tuple_count_list
print(get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"]))
#result expected: [('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]
def get_tuple_count_list(_list):
tuple_count_dict = {}
for v in _list:
if v not in tuple_count_dict:
tuple_count_dict[v] = 1
continue
tuple_count_dict[v] = tuple_count_dict[v] + 1
return list(tuple_count_dict.items())
Python 元组是不可变的,在列表中搜索项目的时间复杂度是 O(n)。用dict替换list of tuple可以提高性能
如何简单地使用 Counter.most_common()
,它是一个标准库实用程序,可以准确地生成您想要的输出:
from collections import Counter
def get_tuple_count_list(_list):
return Counter(_list).most_common()
>>> get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"])
[('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]
另请参阅 Counter
文档。
即使使用 plainer 也意味着您在进行计数时不应对元组列表进行操作。元组的不变性和列表的线性搜索在代码可读性和性能方面都是很大的障碍。您应该始终使用恒定时间查找结构(通常是像 Counter
这样的字典):
def get_tuple_count_list(_list):
counts = {}
for x in _list:
counts[x] = counts.get(x, 0) + 1
return [*counts.items()]
# return sorted(counts.items(), key=lambda i: -i[1])
def get_tuple_count_list(_list):
output = []
for i in _list:
count_val = _list.count(i)
if (i,count_val) not in output:
output.append((i,count_val))
return output
基于列表,我需要创建一个元组列表,每个元组包含(值,nbr_of_occurence_of_the_value_in_the_list)。 我的代码可以正常工作,但我觉得它可以改进,有人知道如何改进这段代码吗?
def get_tuple_count_list(_list):
tuple_count_list = []
for v in _list:
if v not in [v1 for (v1,count) in tuple_count_list]:
tuple_count_list.append((v,1))
continue
i = [v1 for (v1,count) in tuple_count_list].index(v)
tuple_count_list[i] = (v, tuple_count_list[i][1]+1)
return tuple_count_list
print(get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"]))
#result expected: [('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]
def get_tuple_count_list(_list):
tuple_count_dict = {}
for v in _list:
if v not in tuple_count_dict:
tuple_count_dict[v] = 1
continue
tuple_count_dict[v] = tuple_count_dict[v] + 1
return list(tuple_count_dict.items())
Python 元组是不可变的,在列表中搜索项目的时间复杂度是 O(n)。用dict替换list of tuple可以提高性能
如何简单地使用 Counter.most_common()
,它是一个标准库实用程序,可以准确地生成您想要的输出:
from collections import Counter
def get_tuple_count_list(_list):
return Counter(_list).most_common()
>>> get_tuple_count_list(["a","b","b","d","e","a","a","a","c","b"])
[('a', 4), ('b', 3), ('d', 1), ('e', 1), ('c', 1)]
另请参阅 Counter
文档。
即使使用 plainer 也意味着您在进行计数时不应对元组列表进行操作。元组的不变性和列表的线性搜索在代码可读性和性能方面都是很大的障碍。您应该始终使用恒定时间查找结构(通常是像 Counter
这样的字典):
def get_tuple_count_list(_list):
counts = {}
for x in _list:
counts[x] = counts.get(x, 0) + 1
return [*counts.items()]
# return sorted(counts.items(), key=lambda i: -i[1])
def get_tuple_count_list(_list):
output = []
for i in _list:
count_val = _list.count(i)
if (i,count_val) not in output:
output.append((i,count_val))
return output