简体   繁体   English

如何有效计算列表中元素的非连续出现次数?

[英]How to effciently calculate non-consecutive number of appereances of element in list?

So I'm trying to find out how many times a certain element is non consecutively appearing in a list.所以我试图找出某个元素没有连续出现在列表中的次数。 By that I mean:我的意思是:

list = [10,10,10,11,12,10,12,14,10,10,10]
element_searched = 10

=> expected_output = 3

So that means that 10 is appearing 3 times in a the list.所以这意味着 10 在列表中出现了 3 次。

My code so far that seems to be working:到目前为止,我的代码似乎有效:

elements = [11, 10, 12]
row = [10,10,10,10,10,10,10,10,10,11,11,11,11,11,10,10,10,10,12,12,12,12,12,11,11,11,11,12,12,12,12,10]

element_on = False
for element in elements:
    sequence = 0
    for i in range(len(row)):
        if element == row[i] and element_on==False:
            sequence += 1
            element_on = True
        elif element==row[i] and element_on==True:
            pass
        elif element != row[i] and element_on==True:
            element_on = False
        #
        elif element != row[i] and element_on == False:
            element_on = False
        #
    print(f"For element {element} the number ob sequences is: {sequence} ")

I am getting the desired output but I am wondering if there is a more elegant and especially a faster way.我得到了所需的输出,但我想知道是否有更优雅,尤其是更快的方法。

Try this:尝试这个:

row = [10,10,10,10,10,10,10,10,10,11,11,11,11,11,10,10,10,10,12,12,12,12,12,11,11,11,11,12,12,12,12,10]
sr = pd.Series(row, name = "x")
sr[sr.groupby(sr.shift(-1).bfill(0).ne(sr)).transform('cumcount')==1].value_counts()

Output:输出:

10    3
12    2
11    2

First column is x value, second is number of sequences.第一列是x值,第二列是序列数。

More compact and faster way:更紧凑、更快捷的方式:

from  itertools import groupby    
pd.Series([k for k, g in groupby(row)]).value_counts()

Another solution:另一种解决方案:

np.unique([k for k, g in groupby(row)], return_counts=True)

Result:结果:

(array([10, 11, 12]), array([3, 2, 2], dtype=int64))

Alternatively use np.bincount :或者使用np.bincount

np.bincount([k for k, g in groupby(row)])

But the output will be slightly different:但输出会略有不同:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2], dtype=int64)

I think this is what you want.我想这就是你想要的。 Groupby the list by similiar elements and then sum the counts按相似元素对列表进行分组,然后对计数求和

import itertools

element_searched = 10

expected_output = sum([i.count(element_searched) for i in itertools.groupby(list)])

3

I would use a simple dictionary:我会使用一个简单的字典:

row = [10,10,10,10,10,10,10,10,10,11,11,11,11,11,10,10,10,10,12,12,12,12,12,11,11,11,11,12,12,12,12,10]

counter = {}
last_item = None
for item in row:
    if last_item != item:
        counter[item] = counter.get(item, 0) + 1
        last_item = item

print (counter)

A few thoughts which should guide you:一些可以指导您的想法:

  • you need a variable which will store the previous value你需要一个变量来存储以前的值
  • a map of elements, the key being the value of the element, the value being the number of occurrences found so far元素的映射,键是元素的值,值是到目前为止找到的出现次数
  • on each iteration check if the current element equals the previous element and if not, then increment the map item having the current element as key在每次迭代中检查当前元素是否等于前一个元素,如果不是,则增加以当前元素为键的地图项
  • set previous to current value at each step在每一步将上一个设置为当前值

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

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