Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 l = [1,2,2,3,1,1,2,3,4,5,6] 不想使用 l.count(element) 方法的列表。我不想使用 for 循环或迭代器。 输出像 {1: 3, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1}
l = [1,2,2,3,1,1,2,3,4,5,6]
l.count(element)
{1: 3, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1}
使用集合的Counter:
Counter
>>> from collections import Counter >>> l = [1,2,2,3,1,1,2,3,4,5,6] >>> Counter(l) Counter({1: 3, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1})