簡體   English   中英

python將列表拆分為某些字符串的子列表

[英]python split the list into sublist at certain strings

我有一個清單清單:

[['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'],
 ['****']]

我想在list元素為****位置將整個列表拆分為子列表。

因此輸出應如下所示:

[['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']]

謝謝,

您可以使用.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

在element為['****']時拆分:

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)

您可以將其用作參考給定['****']僅是模式

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:])

使用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']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM