Python:如何 return 包含重复项的列表列表中具有计数的不同项目列表?
Python: How to return a list of distinct items with counts from a list of lists that includes duplicates?
我在列表中嵌套了列表。这是我尝试使用列表的迷你预览来计算单个嵌套列表的数量的代码:
count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
distinct_list = []
count_list.sort()
for x in count_list:
if x not in distinct_list:
distinct_list.append(x)
for z in distinct_list:
for y in count_list:
if y == z:
print(z, count_list.count(z))
我正在尝试将输出设为仅包含不同嵌套列表及其计数的字典/列表,以便我可以按计数对结果进行排序。列表不采用两个值,因此我正在考虑将其设为字典以便于存储。
这些是我从上面的代码中得到的结果。
(['a', 'b'], 2)
(['a', 'b'], 2)
(['c'], 2)
(['c'], 2)
(['d', 'e'], 1)
(['e'], 1)
我希望输出只为每个不同的列表及其各自的计数提供一行(而不是为上面的当前输出中看到的同一列表提供多行)。有什么想法吗?
你应该使用 collections.Counter()
:
>>> count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
>>> import collections
>>> result = collections.Counter(tuple(x) for x in count_list)
>>> print(result)
Counter({
('c',): 2,
('a', 'b'): 2,
('d', 'e'): 1,
('e',): 1
})
如果您愿意将嵌套列表转换为元组以便将它们用作键,那么将结果作为字典获取(符合您关于如何解决此问题的最初想法之一)也将相当简单。例如:
data = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
results = {}
for d in data:
t = tuple(d)
if t not in results:
results[t] = data.count(d)
print(results)
# OUTPUT
# {('a', 'b'): 2, ('d', 'e'): 1, ('e',): 1, ('c',): 2}
如果您不想使用任何库,即使 Counter
是 python 的原生库,您也可以...
arr = [[1, 2], [3], [1, 2], [4], [3]]
new_arr = []
for elem in arr:
# You are wrapping your element in a list, at the same scale
# than the list you wish to output (meaning 3 dimensions depth list).
# In other words, you have a list... that contains a list, containing
# your element and its count. The first depth seems useless at first glance,
# but it's just here to keep the same depth you will have appending to new_arr.
# You can try by yourself doing "obj = [elem, arr.count(elem)]" if you don't
# believe me.
obj = [ [elem, arr.count(elem)] ]
if obj[0] not in new_arr:
# checking on first index to check our elem and not our ""wrapper""
new_arr += obj
print(new_arr)
# [[[1, 2], 2], [[3], 2], [[4], 1]]
如果你转换成元组你可以使用字典理解
count_list = [tuple(i) for i in count_list]
d = {i: count_list.count(i) for i in set(count_list)}
{('a', 'b'): 2, ('d', 'e'): 1, ('c',): 2, ('e',): 1}
我在列表中嵌套了列表。这是我尝试使用列表的迷你预览来计算单个嵌套列表的数量的代码:
count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
distinct_list = []
count_list.sort()
for x in count_list:
if x not in distinct_list:
distinct_list.append(x)
for z in distinct_list:
for y in count_list:
if y == z:
print(z, count_list.count(z))
我正在尝试将输出设为仅包含不同嵌套列表及其计数的字典/列表,以便我可以按计数对结果进行排序。列表不采用两个值,因此我正在考虑将其设为字典以便于存储。 这些是我从上面的代码中得到的结果。
(['a', 'b'], 2)
(['a', 'b'], 2)
(['c'], 2)
(['c'], 2)
(['d', 'e'], 1)
(['e'], 1)
我希望输出只为每个不同的列表及其各自的计数提供一行(而不是为上面的当前输出中看到的同一列表提供多行)。有什么想法吗?
你应该使用 collections.Counter()
:
>>> count_list = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
>>> import collections
>>> result = collections.Counter(tuple(x) for x in count_list)
>>> print(result)
Counter({
('c',): 2,
('a', 'b'): 2,
('d', 'e'): 1,
('e',): 1
})
如果您愿意将嵌套列表转换为元组以便将它们用作键,那么将结果作为字典获取(符合您关于如何解决此问题的最初想法之一)也将相当简单。例如:
data = [['a', 'b'],['d', 'e'], ['e'], ['c'], ['a', 'b'], ['c']]
results = {}
for d in data:
t = tuple(d)
if t not in results:
results[t] = data.count(d)
print(results)
# OUTPUT
# {('a', 'b'): 2, ('d', 'e'): 1, ('e',): 1, ('c',): 2}
如果您不想使用任何库,即使 Counter
是 python 的原生库,您也可以...
arr = [[1, 2], [3], [1, 2], [4], [3]]
new_arr = []
for elem in arr:
# You are wrapping your element in a list, at the same scale
# than the list you wish to output (meaning 3 dimensions depth list).
# In other words, you have a list... that contains a list, containing
# your element and its count. The first depth seems useless at first glance,
# but it's just here to keep the same depth you will have appending to new_arr.
# You can try by yourself doing "obj = [elem, arr.count(elem)]" if you don't
# believe me.
obj = [ [elem, arr.count(elem)] ]
if obj[0] not in new_arr:
# checking on first index to check our elem and not our ""wrapper""
new_arr += obj
print(new_arr)
# [[[1, 2], 2], [[3], 2], [[4], 1]]
如果你转换成元组你可以使用字典理解
count_list = [tuple(i) for i in count_list]
d = {i: count_list.count(i) for i in set(count_list)}
{('a', 'b'): 2, ('d', 'e'): 1, ('c',): 2, ('e',): 1}