[英]How to find index of word starting and ending an element in a list? Python
[英]How to find the starting and ending index of elements in list automatically in python
我想在列表中找到所有 userId 的开始和结束索引,我想在不指定每个 userId 的情况下执行此操作,因为数据集很大。
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1.......213,213,213,213]
我希望 output 成为
[{1: 0, 20},{2: 21, 40}.....{213: 29,703, 30,000}]
是否有 package 或 function 可以在 python 中自动执行此操作?
你可以这样做:
from collections import Counter
a = ...
a_counter = Counter(a)
a_indices = []
running_count = 0
for x, x_count in sorted(a_counter.items()):
a_indices.append({x: (running_count, running_count + x_count - 1)})
running_count += x_count
例如,如果a = [1, 1, 2, 2, 3, 3]
, a_indices = [{1: (0, 1)}, {2: (2, 3)}, {3: (4, 5)}]
(最接近您的 output 格式,同时有效)。
如果您愿意稍微更改 output 格式,请使用:
a_indices = {}
running_count = 0
for x, x_count in sorted(a_counter.items()):
a_indices[x] = (running_count, running_count + x_count - 1)
running_count += x_count
现在a_indices
,对于上面的a
,将是{1: (0, 1), 2: (2, 3), 3: (4, 5)}
,这是一个更好的结构。
这两种解决方案都将使x
的每个结束索引都包含在内。 如果要使其独占,请将running_count + x_count - 1
替换为running_count + x_count
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.