简体   繁体   English

捕获事件并在列表中找到它的计数

[英]capturing occurrences and finding its count in a list

I have a situation where a list of last occurrences is available.我有一种情况,可以使用最后一次出现的列表。 Say the list is.说清单是。 ['user', 'user', 'sys', 'sys', 'user', 'user', 'sys', 'user'] So the ask is if a user has occurred how many times did it occur consecutively? ['user', 'user', 'sys', 'sys', 'user', 'user', 'sys', 'user']所以问题是一个用户是否已经出现,它连续出现了多少次? if there is a change in element say 'sys' instead of 'user' the count should start fresh.如果元素发生变化,请说“sys”而不是“user”,计数应该重新开始。 The output I am looking for is [('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)] this would help me identify the pattern the user and system is following.我正在寻找的 output 是[('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)]这会帮助我确定用户和系统遵循的模式。 Any help is much appreciated.任何帮助深表感谢。

Use itertools.groupby :使用itertools.groupby

from itertools import groupby

lst = ['user', 'user', 'sys', 'sys', 'user', 'user', 'sys', 'user']

out = [(value, sum(1 for _ in group)) for value, group in groupby(lst)]
print(out)

Prints:印刷:

[('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)]

You could install the more-itertools package and use its run_length.encode function:您可以安装more-itertools package 并使用其run_length.encode function:

In [1]: from more_itertools import run_length

In [2]: list(run_length.encode(['user', 'user', 'sys', 'sys', 'user', 'user', 'sys', 'user']))
Out[2]: [('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)]

You can iterate over the list and count the values using the stack phenomenon without any additional library.您可以迭代列表并使用堆栈现象计算值,而无需任何额外的库。 Below is the logic:以下是逻辑:

l = ['user', 'user', 'sys', 'sys', 'user', 'user', 'sys', 'user']

f = []
x = [l[0], 0]

for i in l:
    if i == x[0]:
        x[1] += 1
    else:
        f.append(tuple(x))
        # Resetting the value
        x = [i, 1]

# Adding the last iteration value
f.append(tuple(x))

print(f)
        

where, x = temporary list which keeps a track of count of topmost value.其中,x = 临时列表,用于跟踪最高值的计数。 Initially, I have started with zero as I am looping first value again.最初,我从零开始,因为我再次循环第一个值。

Output: f -> [('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)] Output: f -> [('user', 2), ('sys', 2), ('user', 2), ('sys', 1), ('user', 1)]

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

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