Python 如何在列表中列出元素的有序频率
所以我想做一个函数,它给出一个Python 如何在列表中列出元素的有序频率,python,list,Python,List,所以我想做一个函数,它给出一个list作为返回值,这些名称的顺序频率。基本上,所有这些子列表都代表一对朋友,我想列出一个最受欢迎的人到最不受欢迎的人的列表。 我尝试了很多方法,但这些子列表给我带来了问题 a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy
list
作为返回值,这些名称的顺序频率。基本上,所有这些子列表都代表一对朋友,我想列出一个最受欢迎的人到最不受欢迎的人的列表。
我尝试了很多方法,但这些子列表给我带来了问题
a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
所以我们有一个列表。我们可以像下面这样迭代它们:
对于列表中的列表:
对于列表中的名称:
....
然后,我们可以继续计算姓名,并在口述中记录频率。因此我们有一个列表。我们可以像下面这样迭代它们:
对于列表中的列表:
对于列表中的名称:
....
然后,我们可以继续计算姓名,并在口述中记录频率。您可以先用两个重叠的循环将列表展平,然后使用a获得最常见姓名的排序列表:
from collections import Counter
a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
flattened = [name for sublist in a for name in sublist]
counter = Counter(flattened)
out = [name for name, count in counter.most_common()]
print(out)
#['Lucas', 'Emma', 'Peter', 'Marie', 'Patsy', 'Kevin', 'Julie', 'Suzy', 'Tobias']
如果不允许导入任何内容,您可以自己创建计数器,然后按计数对其元素进行排序:
a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
# We flatten the list
flattened = [name for sublist in a for name in sublist]
# ['Marie', 'Lucas', 'Lucas', 'Patsy', 'Emma', 'Lucas', ...]
# We count the names in this list
counter = {}
for name in flattened:
if name not in counter:
counter[name] = 0
counter[name] += 1
# dict.items() returns tuples (key, value), we sort by value
out = [name for name, count in sorted(counter.items(), key=lambda item: item[1], reverse=True)]
print(out)
#['Lucas', 'Emma', 'Peter', 'Marie', 'Patsy', 'Kevin', 'Julie', 'Suzy', 'Tobias']
您可以首先使用两个叠瓦循环展平列表,然后使用a获得最常用名称的排序列表:
from collections import Counter
a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
flattened = [name for sublist in a for name in sublist]
counter = Counter(flattened)
out = [name for name, count in counter.most_common()]
print(out)
#['Lucas', 'Emma', 'Peter', 'Marie', 'Patsy', 'Kevin', 'Julie', 'Suzy', 'Tobias']
如果不允许导入任何内容,您可以自己创建计数器,然后按计数对其元素进行排序:
a = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
# We flatten the list
flattened = [name for sublist in a for name in sublist]
# ['Marie', 'Lucas', 'Lucas', 'Patsy', 'Emma', 'Lucas', ...]
# We count the names in this list
counter = {}
for name in flattened:
if name not in counter:
counter[name] = 0
counter[name] += 1
# dict.items() returns tuples (key, value), we sort by value
out = [name for name, count in sorted(counter.items(), key=lambda item: item[1], reverse=True)]
print(out)
#['Lucas', 'Emma', 'Peter', 'Marie', 'Patsy', 'Kevin', 'Julie', 'Suzy', 'Tobias']
使用集合和itertools,我们可以创建一个简单易读的解决方案
from itertools import chain
from collections import Counter
my_list = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
#we first convert the list of lists to a single list
my_list_flattened = chain.from_iterable(a)
#count the number of occurances
my_list_count = Counter(my_list_flattened)
#finally for the order we can use most_common()
print(my_list_count.most_common())
#[('Lucas', 3), ('Emma', 2), ('Marie', 1), ('Patsy', 1), ('Kevin', 1)]
使用集合和itertools,我们可以创建一个简单易读的解决方案
from itertools import chain
from collections import Counter
my_list = [['Marie', 'Lucas'], ['Lucas', 'Patsy'], ['Emma', 'Lucas'], ['Emma', 'Kevin'], ['Peter', 'Emma'], ['Peter', 'Lucas'], ['Peter', 'Julie'], ['Suzy', 'Tobias']]
#we first convert the list of lists to a single list
my_list_flattened = chain.from_iterable(a)
#count the number of occurances
my_list_count = Counter(my_list_flattened)
#finally for the order we can use most_common()
print(my_list_count.most_common())
#[('Lucas', 3), ('Emma', 2), ('Marie', 1), ('Patsy', 1), ('Kevin', 1)]
很抱歉,我没有明确表示我无法使用导入来解决此问题,您需要做很多更改吗?您不能使用导入吗?非常感谢。这是一个漂亮的编码,我喜欢它。不客气:)如果你觉得答案已经回答了你的问题,你可以接受它(左边打勾)和/或向上投票(一旦你有足够的声誉(15)),我已经向上投票了你的答案,它说它没有公开显示,因为我确实没有15个声誉,但我的投票记录了下来,我不知道复选标记,但现在我检查了它。再次感谢!:)很抱歉,我没有明确表示我无法使用导入来解决此问题,您需要做很多更改吗?您不能使用导入吗?非常感谢。这是一个漂亮的编码,我喜欢它。不客气:)如果你觉得答案已经回答了你的问题,你可以接受它(左边打勾)和/或向上投票(一旦你有足够的声誉(15)),我已经向上投票了你的答案,它说它没有公开显示,因为我确实没有15个声誉,但我的投票记录了下来,我不知道复选标记,但现在我检查了它。再次感谢!:)