python将列表拆分为某些字符串的子列表

时间:2017-09-26 06:53:59

标签: python

我有一份清单清单:

[['H', '0'], ['S', '3', '1.00'],
 ['33.8650000', '0.0254938'],
 ['5.0947900', '0.1903730'],
 ['1.1587900', '0.8521610'],
 ['S', '1', '1.00'],
 ['0.3258400', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.1027410', '1.0000000'],
 ['****'],
 ['He', '0'], ['S', '3', '1.00'],
 ['98.1243000', '0.0287452'],
 ['14.7689000', '0.2080610'],
 ['3.3188300', '0.8376350'],
 ['S', '1', '1.00'],
 ['0.8740470', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.2445640', '1.0000000'],
 ['****']]

我想将整个列表拆分为列表元素为****的点的子列表。

所以输出应该如下:

[['H', '0'], ['S', '3', '1.00'],
 ['33.8650000', '0.0254938'],
 ['5.0947900', '0.1903730'],
 ['1.1587900', '0.8521610'],
 ['S', '1', '1.00'],
 ['0.3258400', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.1027410', '1.0000000'],
 # the ['****'] was here!
 ]

[['He', '0'], ['S', '3', '1.00'],
 ['98.1243000', '0.0287452'],
 ['14.7689000', '0.2080610'],
 ['3.3188300', '0.8376350'],
 ['S', '1', '1.00'],
 ['0.8740470', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.2445640', '1.0000000']]

谢谢,

4 个答案:

答案 0 :(得分:2)

您可以使用.index()方法在列表中查找['****']。但请注意,如果找不到该元素,它会抛出ValueException,因此您必须使用tryexcept块来捕获它。

lsts = [['H', '0'], ['S', '3', '1.00'], ['33.8650000', '0.0254938'], ['5.0947900', '0.1903730'], ['1.1587900', '0.8521610'], ['S', '1', '1.00'], ['0.3258400', '1.0000000'], ['S', '1', '1.00'], ['0.1027410', '1.0000000'], ['****'], ['He', '0'], ['S', '3', '1.00'], ['98.1243000', '0.0287452'], ['14.7689000', '0.2080610'], ['3.3188300', '0.8376350'], ['S', '1', '1.00'], ['0.8740470', '1.0000000'], ['S', '1', '1.00'], ['0.2445640', '1.0000000'], ['****']]
split_lsts = []
while True:
    try:
        index = lsts.index(['****'])
        split_lsts.append(lsts[:index])
        lsts = lsts[index+1:]
    except ValueError:
        break

答案 1 :(得分:0)

元素为[' ****']时分割:

a = [['H', '0'], ['S', '3', '1.00'], ['33.8650000', '0.0254938'], ['5.0947900', '0.1903730'], ['1.1587900', '0.8521610'], ['S', '1', '1.00'], ['0.3258400', '1.0000000'], ['S', '1', '1.00'], ['0.1027410', '1.0000000'], ['****'], ['He', '0'], ['S', '3', '1.00'], ['98.1243000', '0.0287452'], ['14.7689000', '0.2080610'], ['3.3188300', '0.8376350'], ['S', '1', '1.00'], ['0.8740470', '1.0000000'], ['S', '1', '1.00'], ['0.2445640', '1.0000000'], ['****']]
new_list=[]
for el in a:
    if el == ['****']:
        print new_list
        new_list = []
        continue
    else:
        new_list.append(el)

答案 2 :(得分:0)

您可以将此作为参考使用[' ****']仅为模式

a = [['H', '0'], ['S', '3', '1.00'],
 ['33.8650000', '0.0254938'],
 ['5.0947900', '0.1903730'],
 ['1.1587900', '0.8521610'],
 ['S', '1', '1.00'],
 ['0.3258400', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.1027410', '1.0000000'],
 ['****'],
 ['He', '0'], ['S', '3', '1.00'],
 ['98.1243000', '0.0287452'],
 ['14.7689000', '0.2080610'],
 ['3.3188300', '0.8376350'],
 ['S', '1', '1.00'],
 ['0.8740470', '1.0000000'],
 ['S', '1', '1.00'],
 ['0.2445640', '1.0000000'],
 ['****']]

  mid = a.index(['****'])
  print(a[:mid])
  print(a[mid+1:])

答案 3 :(得分:0)

使用itertools.takewhile功能:

import itertools

result = []
idx = 0
while idx < len(lst):   # lst is your initial list of sublists
    result.append(list(itertools.takewhile(lambda l: l[0] != '****', lst if not result else lst[idx:])))
    idx += len(lst) if not result[0] else len(result[-1]) + 1

输出生成的子列表:

for sub_l in result:
    print(sub_l)

输出:

[['H', '0'], ['S', '3', '1.00'], ['33.8650000', '0.0254938'], ['5.0947900', '0.1903730'], ['1.1587900', '0.8521610'], ['S', '1', '1.00'], ['0.3258400', '1.0000000'], ['S', '1', '1.00'], ['0.1027410', '1.0000000']]
[['He', '0'], ['S', '3', '1.00'], ['98.1243000', '0.0287452'], ['14.7689000', '0.2080610'], ['3.3188300', '0.8376350'], ['S', '1', '1.00'], ['0.8740470', '1.0000000'], ['S', '1', '1.00'], ['0.2445640', '1.0000000']]