我正在尝试获取由(item_number, fruit)元组组成的列表,并计算每种水果出现在列表中的次数。使用collections.Counter. 我正在使用most_common()它。
我遇到的问题是,当试图同时显示与特定类型水果相对应的 item_numbers 列表时,它们会变得乱七八糟。
这是我的示例代码:
#!/usr/bin/env python
from collections import Counter, defaultdict
mylist = [
(1, 'peach'),
(2, 'apple'),
(3, 'orange'),
(4, 'apple'),
(5, 'banana'),
(6, 'apple'),
(7, 'orange'),
(8, 'peach'),
(9, 'apple'),
(10, 'orange'),
(11, 'plum'),
]
# FIRST, HANDLE JUST COUNTING THE ITEMS
normal_list = []
# append to a simple list
for item_number, fruit in mylist:
normal_list.append(fruit)
# prints just the name of each fruit and how many times it appears
for fruit, count in Counter(normal_list).most_common(10):
print(f'{fruit}\tCount: {count}')
# NOW TRY TO INCLUDE THE LIST IF ITEM NUMBERS ALSO
mydefaultdict = defaultdict(list)
# append to the defaultdict
for item_number, fruit in mylist:
mydefaultdict[fruit].append(item_number)
# prints each fruit, followed by count, and finally the list of IPs for each
for fruit, item_list in Counter(mydefaultdict).most_common(10):
print(f'{fruit}\tCount: {len(item_list)}\tList: {item_list}')
我得到了更简单版本的预期输出:
apple Count: 4
orange Count: 3
peach Count: 2
banana Count: 1
plum Count: 1
但是,当我尝试向其中添加 item_number 列表时,结果不再排序,当我使用most_common()小于水果品种总数的值时,这会造成严重破坏:
plum Count: 1 List: [11]
banana Count: 1 List: [5]
orange Count: 3 List: [3, 7, 10]
apple Count: 4 List: [2, 4, 6, 9]
peach Count: 2 List: [1, 8]
我确信在这里我可以做一些不同的事情,但我不太确定是什么。
繁星coding
手掌心
相关分类