[英]Counting identical list-elements in a row in Python
I am using Python3.我正在使用 Python3。 I have a list a
of only integers.我有a
只有整数的列表。 Now, I want to save the element and the number it repeats itself in a row in another list.现在,我想保存元素和它在另一个列表中连续重复的数字。
Example:例子:
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
Output: Output:
result = ["6,1", "0, 2", "2, 4", "1, 1", "89, 2"]
# the number before the "," represents the element, the number after the "," represents how many times it repeats itself.
How to efficiently achieve my goal?如何有效地实现我的目标?
I believe all the solutions given are counting the total occurrences of a number in the list rather than counting the repeating runs of a number.我相信给出的所有解决方案都是计算列表中某个数字的总出现次数,而不是计算一个数字的重复运行次数。
Here is a solution using groupby from itertools.这是使用来自 itertools 的 groupby 的解决方案。 It gathers the runs and appends them to a dictionary keyed by the number.它收集运行并将它们附加到以数字为键的字典中。
from itertools import groupby
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
d = dict()
for k, v in groupby(a):
d.setdefault(k, []).append(len(list(v)))
Dictionary created:创建的字典:
>>> d
{6: [1], 0: [2], 2: [4], 1: [1], 89: [2]}
Note that all runs only had 1 count in their list.请注意,所有运行的列表中只有 1 个计数。 If there where other occurrences of a number already seen, there would be multiple counts in the lists (that are the values for dictionary).如果已经看到其他出现的数字,则列表中将有多个计数(即字典的值)。
for counting an individual element, us list.count
,用于计算单个元素,我们list.count
,
ie, here, for, say 2
, we user a.count(2)
, which outputs 4
,即,在这里,比如说2
,我们用户a.count(2)
,它输出4
,
also, set(a)
gives the unique elements in a
此外, set(a)
给出了a
中的唯一元素
overall answer,总体答案,
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
nums = set(a)
result = [f"{val}, {a.count(val)}" for val in set(a)]
print(result)
which gives这使
['0, 2', '1, 1', '2, 4', '6, 1', '89, 2']
Method 1: using for loop方法一:使用for循环
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
result = []
a_set = set(a) # transform the list into a set to have unique integer
for nbr in a_set:
nbr_count = a.count(nbr)
result.append("{},{}".format(nbr, nbr_count))
print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']
Method 2: using list-comprehensions方法2:使用列表理解
result = ["{},{}".format(item, a.count(item)) for item in set(a)]
print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']
you can use Python List count()
Method, method returns the number of elements with the specified value.可以使用 Python 列表count()
方法,方法返回具有指定值的元素个数。
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
print ({x:a.count(x) for x in a})
output: output:
{6: 1, 0: 2, 2: 4, 1: 1, 89: 2}
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
dic = dict()
for i in a:
if(i in dic):
dic[i] = dic[i] + 1
else:
dic[i] = 1
result = []
for i in dic:
result.append(str(i) +"," + str(dic[i]))
Or:或者:
from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
mylist = [Counter(a)]
print(mylist)
You can use Counter
from collections:您可以使用 collections 中的Counter
:
from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
counter = Counter(a)
result = ['{},{}'.format(k, v) for k,v in counter.items()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.