简体   繁体   English

创建一个列表,计算另一个列表中数字的连续出现次数

[英]Create a list counting sequential occurrences of numbers in another list

I have this list我有这个清单

x = [2,3,4,2,2]

I want to create a new list of the same size where each element will represent a count of the number of times the corresponding number has appeared in the original list before the current element.我想创建一个相同大小的新列表,其中每个元素将表示相应数字在当前元素之前出现在原始列表中的次数的计数。 For example, the first 3 elements are 2,3,4 So, I want the first 3 values in the new list to be 0, and in x[3] since 2 is repeated, I want the value to be 1 and x[4] it's again 2 I want the value to be 2 (the number 2 has been encountered in the list twice up to this point).例如,前 3 个元素是 2,3,4 所以,我希望新列表中的前 3 个值是 0,而在x[3]中,由于 2 重复,我希望值是 1 和x[4]又是 2 我希望值为 2(到目前为止,在列表中已经两次遇到数字 2)。

My expected answer is the new list = [0,0,0,1,2]我的预期答案是新列表 = [0,0,0,1,2]

Similarly for the list [2,3,4,2,2,3,3] I want the new list to be [0,0,0,1,2,1,2]同样对于列表[2,3,4,2,2,3,3]我希望新列表为[0,0,0,1,2,1,2]

You can leverage itertools.count and collections.defaultdict to make a simple, efficient list comprehension.您可以利用itertools.countcollections.defaultdict来进行简单、高效的列表理解。 By setting count() as the default value for your default dictionary you get a new counter item for every unique number in the list that increments as you call next() on it.通过将count()设置为默认字典的默认值,您可以为列表中的每个唯一数字获得一个新的计数器项,该计数器项在您调用next()时递增。

from itertools import count
from collections import defaultdict

c = defaultdict(count)
x = [2,3,4,2,2]

[next(c[n]) for n in x ]
# [0, 0, 0, 1, 2]

You can keep track of the number of times a number has been met and add it to the result accordingly.您可以跟踪某个数字被满足的次数,并相应地将其添加到结果中。

>>> d = defaultdict(int)
>>> res = []
>>> for el in x:
...     res.append(d[el])
...     d[el]+=1
... 
>>> res
[0, 0, 0, 1, 2]

I don't see any shortcuts to solving this other than full exploration.除了全面探索之外,我看不到任何解决此问题的捷径。

counters = {} # keep track of counts
x= [2,3,4,2,2]
new_list = [counters[c] for c in x if not counters.update({c:counters.get(c,-1)+1})]

I came up with this one:我想出了这个:

print([x[:i].count(x[i]) for i in range(len(x))])

First iterate over the index of the list: for i in range(len) Then take the value at position i of the original list and count how many times it occurs before that position.首先遍历列表的索引: for i in range(len)然后取原始列表的 position i处的值,并计算它在该 position之前出现的次数。 For the original list x = [2,3,4,2,2] we obtain:对于原始列表x = [2,3,4,2,2] ,我们得到:

i=0  [].count(2) -> 0
i=1  [2].count(3) -> 0
i=2  [2,3].count(4) -> 0
i=3  [2,3,4].count(2) -> 1
i=4  [2,3,4,2].count(2) -> 2

A shorter but probably less efficient solution:一个较短但可能效率较低的解决方案:

[x[:i].count(n) for i,n in enumerate(x)]

You could try this as well:你也可以试试这个:

list = [2, 3, 4, 2, 2]
repeats = []
repeatsCounter = 0
for i in range(len(list)): #loops through the list
   for j in range(i - 1, -1, -1): #loops through every value until i
      if list[i] == list[j]:
         repeatsCounter += 1
         break
   repeats.append(repeatsCounter)
   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM