Python中的拆分整数列表
我有一个整数列表。我想将主列表拆分为多个非零整数的子列表 例如:Python中的拆分整数列表,python,Python,我有一个整数列表。我想将主列表拆分为多个非零整数的子列表 例如: main_lists = [0,0,0,0,0,0,123,432,5,54,0,654,645,34,23,12,0,0,0,0,0,0,123,1,312,312,132,3,123,0,0,0,0,0,0,0] output = [123,432,5,54,0,654,645,34,23,12] [123,1,312,312,132,3,123] 情况是,, 如果在两个非零值之间发现一个或两个(小于5)零,则应在输出
main_lists = [0,0,0,0,0,0,123,432,5,54,0,654,645,34,23,12,0,0,0,0,0,0,123,1,312,312,132,3,123,0,0,0,0,0,0,0]
output =
[123,432,5,54,0,654,645,34,23,12]
[123,1,312,312,132,3,123]
情况是,,
如果在两个非零值之间发现一个或两个(小于5)零,则应在输出列表中包含这些零。如果发现超过5个连续零,则应将列表从该位置拆分
main_lists = [0,0,0,0,0,0,123,432,5,54,0,654,645,34,23,12,0,0,0,0,0,0,123,1,312,312,132,3,123,0,0,0,0,0,0,0]
zero_count = 0
non_zero_temp = []
for i in main_lists:
if i == 0:
zero_count = zero_count + 1
else:
if zero_count < 5:
non_zero_temp.append(i)
zero_count = 0
main_list=[0,0,0,0,0123432,5,54,0654645,34,23,12,0,0,0,0,0,0123,1312312132,3123,0,0,0,0,0]
零计数=0
非零温度=[]
对于主列表中的i:
如果i==0:
零计数=零计数+1
其他:
如果零计数小于5:
非零温度附加(i)
零计数=0
提前感谢,我将发布一个备选方案:
from itertools import groupby
main_lists = [
0,0,0,0,0,0,123,432,5,54,0,654,645,34,23,12,0,
0,0,0,0,0,123,1,312,312,132,3,123,0,0,0,0,0,0,0
]
# group numbers into contiguous lists (by 0 or not-0)
is_zero = lambda n: not n
groups = (list(nums) for zero,nums in groupby(main_lists, key=is_zero))
# group lists into contiguous chunks (by to-keep or to-discard)
is_keeper = lambda lst: bool(lst[0]) or len(lst) < 5
chunks = (chunk for keep,chunk in groupby(grouped, key=is_keeper) if keep)
# reassemble chunks
final = [[i for lst in chunk for i in lst] for chunk in chunks]
output = []
non_zero_temp = []
length = len(main_lists)
i = 0
while i < length:
zeros = []
while i < length and main_lists[i] == 0:
i += 1
zeros.append(0)
if len(zeros) != 0 and len(zeros) < 5:
non_zero_temp += zeros
elif len(zeros) > 5:
if len(non_zero_temp) > 0 and i < length:
output.append(non_zero_temp)
non_zero_temp = []
else:
non_zero_temp.append(main_lists[i])
i += 1
if len(non_zero_temp) > 0:
output.append(non_zero_temp)
print(output)
[[123, 432, 5, 54, 0, 654, 645, 34, 23, 12], [123, 1, 312, 312, 132, 3, 123]]
output=[]
非零温度=[]
长度=长度(主列表)
i=0
而我<长度:
零=[]
而i5:
如果len(非零温度)>0且i0:
输出.附加(非零温度)
打印(输出)
[[123, 432, 5, 54, 0, 654, 645, 34, 23, 12], [123, 1, 312, 312, 132, 3, 123]]
main_list=[0,0,0,0,0,0123432,5,54,0654645,34,23,12,0,0,0,0,0,0123,1312312132,3123,0,0,0,0,0,0,0,0]zero_count=0非zero_temp=[]对于主_list中的i:if i=0:zero_count=0+1其他:if zero_count<5:non_temp.append(i)zero_cout=0@Ffisegydd完成!这和?@Ffisegydd不完全一样,我在那个问题上出错了……我能做些什么……删除这个问题或将其标记为不相关??对不起…“我在那个问题上错了。”你这是什么意思?如果可能的话,尝试通过编辑来更正问题。您可以使用bool
作为groupby
中的键,而不是lambda。@Blckknght:是的,我使用的是运算符。不是
,但我认为这“更明显”。
output = []
non_zero_temp = []
length = len(main_lists)
i = 0
while i < length:
zeros = []
while i < length and main_lists[i] == 0:
i += 1
zeros.append(0)
if len(zeros) != 0 and len(zeros) < 5:
non_zero_temp += zeros
elif len(zeros) > 5:
if len(non_zero_temp) > 0 and i < length:
output.append(non_zero_temp)
non_zero_temp = []
else:
non_zero_temp.append(main_lists[i])
i += 1
if len(non_zero_temp) > 0:
output.append(non_zero_temp)
print(output)
[[123, 432, 5, 54, 0, 654, 645, 34, 23, 12], [123, 1, 312, 312, 132, 3, 123]]