如何每 N 项拆分一次列表



我正在尝试每 5 个项目拆分一个列表,然后删除接下来的两个项目('nan'(。我尝试使用 List[:5],但这似乎无法在循环中使用。所需输出为:

[['1'、'2'、'3'、'4'、'5']、['1'、'2'、'3'、'4'、'5']、['1'、'2'、'3'、'4'、'5']、['1'、'2'、'3'、'4'、'5']]
List = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
for i in List:
# split first 5 items
# delete next two items
# Desired output:
# [['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5']]

有很多方法可以做到这一点。我建议按 7 步进,然后按 5 分接。

data = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
# Step by 7 and keep the first 5
chunks = [data[i:i+5] for i in range(0, len(data), 7)]
print(*chunks, sep='n')

输出:

['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']

参考:将 python 列表拆分为其他"子列表"...

警告:确保列表遵循您所说的规则,每 5 个项目 2 nan。

此循环会将前 5 个项目添加为列表,并删除前 7 个项目。

lst = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
output = []
while True:
if len(lst) <= 0:
break
output.append(lst[:5])
del lst[:7]
print(output) # [['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5']]
List=['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
new_list = list()
for k in range(len(List)//7):
new_list.append(List[k*7:k*7+5])
new_list.append(List[-len(List)%7])

如果列表不遵循您提到的规则,但您希望始终在 NAN 之间拆分序列,则直接的解决方案:

result, temp = [], []
for item in lst:
if item != 'nan':
temp.append(item)
elif temp:
result.append(list(temp))
temp = []

使用itertools.groupby还可以支持不同长度的块:

[list(v) for k, v in groupby(List, key='nan'.__ne__) if k]

我想还有更多的pythonic方法可以做同样的事情,但是:

result = []
while (len(List) > 5):
result.append(List[0:0+5])
del List[0:0+5]
del List[0:2]

结果:[['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5']]

mainlist=[] 
sublist=[] 
count=0
for i in List:
if i!="nan" :    
if count==4:
# delete next two items
mainlist.append(sublist)
count=0
sublist=[] 
else:
# split first 5 items
sublist.append(i)
count+=1

通常numpy.split(...)会为您进行任何类型的自定义拆分。一些参考:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html

和代码:

import numpy as np
lst = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
ind=np.ravel([[i*7+5, (i+1)*7] for i in range(len(lst)//7)])
lst2=np.split(lst, ind)[:-1:2]
print(lst2)

输出:

[array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3')]

我喜欢拼接答案。

这是我的2美分。

# changed var name away from var type
myList = ['1','2','3','4','5','nan','nan','1','2','3','4','10','nan','nan','1','2','3','4','15','nan','nan','1','2','3','4','20','nan','nan']
newList = []   # declare new list of lists to create
addItem = []   # declare temp list
myIndex = 0    # declare temp counting variable
for i in myList:
myIndex +=1
if myIndex==6:
nothing = 0  #do nothing
elif myIndex==7: #add sub list to new list and reset variables
if len(addItem)>0:
newList.append(list(addItem))
addItem=[]
myIndex = 0
else:
addItem.append(i)
#output
print(newList)

最新更新