[英]Python list traversal with gaps
Hi I have a multidimensional list such as: 嗨,我有一个多维列表,例如:
my_list = [[1,2,3,1,2],[1,0,3,1,2],[1,0,0,0,2],[1,0,3,0,2]]
where 0 represents a gap between two pieces of data. 其中0表示两个数据之间的间隔。
What I need to do is iterate through the list and keep track of how many gaps are in each sublist and throw away the zeros. 我需要做的是遍历列表,并跟踪每个子列表中有多少空白并丢弃零。 I think the best way is to break each sublist into chunks where there are zeros so I end up with smaller lists of integers and a number of gaps.
我认为最好的方法是将每个子列表分成零个块,这样我最终得到的是较小的整数列表和多个间隔。 Ideally, to form a new list which tells me the length of each chunk and number of gaps (ie chunks -1), such as:
理想情况下,形成一个新列表,告诉我每个块的长度和间隙(即块-1)的数量,例如:
new_list = [[5, 0], [[1, 3], 1], [[1, 1], 1], [[1, 1, 1], 2]]
or probably better: 也许更好:
new_list = [[5], [1, 3], [1, 1], [1, 1, 1]]
and I will know that the gaps are equal to len(chunk). 而且我知道差距等于len(chunk)。
EDIT: However, leading and trailing zeros do not represent gaps. 编辑:但是,前导零和尾随零并不表示差距。 ie [0,0,1,2] represents one continuous chunk.
即[0,0,1,2]代表一个连续的块。
Any help much appreciated. 任何帮助,不胜感激。
itertools.groupby()
is perfect for this: itertools.groupby()
非常适合:
from itertools import groupby
my_list = [[1,2,3,1,2],[1,0,3,1,2],[1,0,0,0,2],[1,0,3,0,2]]
new_list = [[len(list(g)) for k, g in groupby(inner, bool) if k] for inner in my_list]
Result: 结果:
>>> new_list
[[5], [1, 3], [1, 1], [1, 1, 1]]
The result contains the length of each non-zero chunk for each sublist, so for example [1,0,3,1,2]
gives [1,3]
, so there are two chunks (one gap). 结果包含每个子列表的每个非零块的长度,因此例如
[1,0,3,1,2]
给出[1,3]
,因此有两个块(一个间隙)。 This matches your second output format. 这与您的第二种输出格式匹配。
Here is my humble code without any imports: 这是我谦虚的代码,没有任何导入:
The algorithm is slightly long: 该算法略长:
def toggle(n):
return n != 0
def chunk_counter(L):
"""
list -> list
"""
chunk_list = []
pivots = []
for j in range(len(L)):
if j == 0 and toggle(L[0]):
pivots.append(j)
elif toggle(L[j]) and toggle(L[j]) != toggle(L[j-1]):
pivots.append(j)
for m in range(len(pivots)):
k = 0
if m == len(pivots)-1:
bound = len(L)
else:
bound = pivots[m+1]
p = 0
while p in range(bound - pivots[m]):
if toggle(L[pivots[m] + p]):
k += 1
p += 1
else:
p += 1
chunk_list.append(k)
return chunk_list
def chunks(L):
"""
(list of lists) -> list of lists
"""
new_list = []
for i in range(len(L)):
new_list.append(chunk_counter(L[i]))
return new_list
So, you may try the function chunks()
on your list: 因此,您可以尝试列表上的功能
chunks()
:
>>> L = [[1,2,3,1,2],[1,0,3,1,2],[1,0,0,0,2],[1,0,3,0,2], [0,0,1,2]]
>>> chunks(L)
[[5], [1, 3], [1, 1], [1, 1, 1], [2]]
Here's a recursive definition (a replacement for Chunk Counter): 这是一个递归定义(代替“块计数器”):
counter_list = []
def counter(L):
k = 0
while(k < len(L) and L[k] != 0):
k +=1
counter_list.append(k)
if k == len(L):
print counter_list
else:
counter(L[k+1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.