python 中的特定值除外,如何按部分保存列表
How to save list by section except specific value in python
我希望这个问题不会重复;找不到相同的问题。
我有以下数据列表
data = ['0.112', '0.442', '-', '0.04', '0.41', '0.11', '0.7', '-', '-', '0.31', '0.9', '0.38', '0.22', '-']
我想按部分存储列表中的数据,但特定值“-”除外,如下所示。
[['0.112', '0.442'],['0.04', '0.41', '0.11', '0.7'],['0.31', '0.9', '0.38', '0.22']]
这段代码可以完成工作。
data = ['0.112', '0.442', '-', '0.04', '0.41', '0.11', '0.7', '-', '-', '0.31', '0.9', '0.38', '0.22', '-']
sublist = []
answer = []
for i in range(len(data)):
if data[i] == '-':
answer.append(sublist)
sublist = []
else:
sublist.append(data[i])
answer = [x for x in answer if x]
print(answer)
我希望这个问题不会重复;找不到相同的问题。
我有以下数据列表
data = ['0.112', '0.442', '-', '0.04', '0.41', '0.11', '0.7', '-', '-', '0.31', '0.9', '0.38', '0.22', '-']
我想按部分存储列表中的数据,但特定值“-”除外,如下所示。
[['0.112', '0.442'],['0.04', '0.41', '0.11', '0.7'],['0.31', '0.9', '0.38', '0.22']]
这段代码可以完成工作。
data = ['0.112', '0.442', '-', '0.04', '0.41', '0.11', '0.7', '-', '-', '0.31', '0.9', '0.38', '0.22', '-']
sublist = []
answer = []
for i in range(len(data)):
if data[i] == '-':
answer.append(sublist)
sublist = []
else:
sublist.append(data[i])
answer = [x for x in answer if x]
print(answer)