计算嵌套列表中列表元素的出现次数
Count occurrences of list element in nested list
我有一个这样的列表:
list_all = [['car', '123464', '4322445'], ['car', '64346', '643267'], ['bicycle','1357','78543'],
['bicycle', '75325', '75425'],
['car', '8652', '652466'], ['taxi', '653367', '63226'],
['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],
['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'],
['motorcycle', '773345', '9977'],
['motorcycle', '3466', '987444']]
我想要这样的结果:
result = [['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]
新列表应该 return 基于第一个元素的元素计数。我已经尝试过这种方式,但它不起作用:
new = []
for item in list_all:
if item[0] != [new[0] for el in new]:
new.append(item)
print(new)
我是 Python 的新手。任何帮助都感激不尽。非常感谢。
这可能最容易实现,只需在每个列表的第一个元素上使用 Counter
:
from collections import Counter
c =Counter([l[0] for l in list_all])
list(c.items())
输出:
[('car', 3), ('bicycle', 2), ('taxi', 3), ('motorcycle', 4)]
如果你真的想要输出中的列表列表(而不是元组列表),请使用列表理解 c.items()
:
[list(i) for i in c.items()]
输出:
[['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]
您可以计算字典中每个内部列表中第一项的出现次数,然后使用列表理解将字典转换为列表。
list_all = [['car', '123464', '4322445'], ['car', '64346', '643267'], ['bicycle','1357','78543'],
['bicycle', '75325', '75425'],
['car', '8652', '652466'], ['taxi', '653367', '63226'],
['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],
['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'],
['motorcycle', '773345', '9977'],
['motorcycle', '3466', '987444']]
def foo(arr):
dct = dict()
for lst in arr:
if lst[0] in dct:
dct[lst[0]] += 1
else:
dct[lst[0]] = 1
return [[k, v] for k,v in dct.items()]
print(foo(list_all))
有些人更愿意使用 collections.defaultdict
来做同样的事情:
from collections import defaultdict
def foo2(arr):
dct = defaultdict(int)
for lst in arr:
dct[lst[0]] += 1
return [[k,v] for k,v in dct.items()]
print(foo2(list_all))
这两个都会输出:
[['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]
我有一个这样的列表:
list_all = [['car', '123464', '4322445'], ['car', '64346', '643267'], ['bicycle','1357','78543'],
['bicycle', '75325', '75425'],
['car', '8652', '652466'], ['taxi', '653367', '63226'],
['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],
['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'],
['motorcycle', '773345', '9977'],
['motorcycle', '3466', '987444']]
我想要这样的结果:
result = [['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]
新列表应该 return 基于第一个元素的元素计数。我已经尝试过这种方式,但它不起作用:
new = []
for item in list_all:
if item[0] != [new[0] for el in new]:
new.append(item)
print(new)
我是 Python 的新手。任何帮助都感激不尽。非常感谢。
这可能最容易实现,只需在每个列表的第一个元素上使用 Counter
:
from collections import Counter
c =Counter([l[0] for l in list_all])
list(c.items())
输出:
[('car', 3), ('bicycle', 2), ('taxi', 3), ('motorcycle', 4)]
如果你真的想要输出中的列表列表(而不是元组列表),请使用列表理解 c.items()
:
[list(i) for i in c.items()]
输出:
[['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]
您可以计算字典中每个内部列表中第一项的出现次数,然后使用列表理解将字典转换为列表。
list_all = [['car', '123464', '4322445'], ['car', '64346', '643267'], ['bicycle','1357','78543'],
['bicycle', '75325', '75425'],
['car', '8652', '652466'], ['taxi', '653367', '63226'],
['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],
['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'],
['motorcycle', '773345', '9977'],
['motorcycle', '3466', '987444']]
def foo(arr):
dct = dict()
for lst in arr:
if lst[0] in dct:
dct[lst[0]] += 1
else:
dct[lst[0]] = 1
return [[k, v] for k,v in dct.items()]
print(foo(list_all))
有些人更愿意使用 collections.defaultdict
来做同样的事情:
from collections import defaultdict
def foo2(arr):
dct = defaultdict(int)
for lst in arr:
dct[lst[0]] += 1
return [[k,v] for k,v in dct.items()]
print(foo2(list_all))
这两个都会输出:
[['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]