繁体   English   中英

Python将列表拆分为给定开始/结束关键字的子列表

[英]Python splitting list to sublists at given start/end keywords

如果我有一个清单,请说

lst = ['hello', 'foo', 'test', 'world', 'bar', 'idk']

我想把它分成一个子列表,其中'foo''bar'作为开始和结束关键字,这样我就可以了

lst = ['hello', ['foo', 'test', 'world', 'bar'], 'idk']

我目前这样做的方式如下。

def findLoop(t):   
    inds = [index for index, item in enumerate(t) if item in ["FOO", "BAR"]]
    centre = inds[(len(inds)/2)-1:(len(inds)/2)+1]
    newCentre = t[centre[0]:centre[1]+1]
    return t[:centre[0]] + [newCentre] + t[centre[1]+1:]

def getLoops(t):
    inds = len([index for index, item in enumerate(t) if item in ["FOO", "BAR"]])
    for i in range(inds):
        t = findLoop(t)
    return t

这看起来有点混乱,但它对于嵌套的开始/结束关键字非常有效,因此可以在子列表中形成子列表,但它不适用于不在彼此内部的多个开始/结束关键字。 嵌套并不重要,所以任何帮助都将受到赞赏。

使用切片的一种方法:

>>> lst = ['hello', 'foo', 'test', 'world', 'bar', 'idk']
>>> a=lst.index('foo')
>>> b=lst.index('bar')+1
>>> lst[a:b] = [lst[a:b]]
>>> lst
['hello', ['foo', 'test', 'world', 'bar'], 'idk']

多个开始,结束(基于Mark Tolonen的回答)

lst = ['hello', 'foo', 'test', 'world', 'bar', 'idk','am']
t = [('foo','test'),('world','idk')]

def sublists(lst, t):
    for start,end in t:
        a=lst.index(start)
        b=lst.index(end)+1
        lst[a:b] = [lst[a:b]]
    return lst

print(sublists(lst,t)) 

返回:

 ['hello', ['foo', 'test'], ['world', 'bar', 'idk'], 'am']

使用切片,不支持嵌套列表:

>>> lst = ['hello', 'foo', 'test', 'world', 'bar', 'idk']
>>> start_idx = lst.index('foo')
>>> end_idx = lst.index('bar')
>>> lst[:start_idx] + [lst[start_idx:end_idx+1]] + lst[end_idx+1:]
['hello', ['foo', 'test', 'world', 'bar'], 'idk']

一种创造性的方法是将列表转储到JSON字符串,在需要的地方添加[] ,并将JSON字符串解析回Python嵌套列表:

import json
lst = ['hello', 'foo', 'test', 'world', 'bar', 'idk']
start_keywords = ['world', 'foo', 'test']
end_keywords = ['bar', 'idk', 'foo']
dump = json.dumps(lst)

for k in start_keywords:
    dump = dump.replace(f'"{k}"', f'["{k}"')

for k in end_keywords:
    dump = dump.replace(f'"{k}"', f'"{k}"]')

json.loads(dump)
# ['hello', ['foo'], ['test', ['world', 'bar'], 'idk']]
json.loads(dump)[2][1][0]
# 'world'

它的优点是易于遵循,它适用于任意嵌套列表,并检测结构是否正确。 但是,你需要确保你的单词不包含"

要使代码获得所需的结果,您需要进行以下更改:

  1. 切片索引必须是整数。 如果您的测试列表具有奇数长度,则您的findLoop函数将在第二行失败。 将切片索引的类型强制转换为int以向下舍入(这里需要)

     centre = inds[int(len(inds)/2)-1:int(len(inds)/2)+1] 
  2. in区分大小写。

     >>> 'foo' in ['FOO', 'BAR'] False 
  3. 在getLoops中,您只需要搜索对中的第一个元素,作为每次调用中一对单词的findLoops子列表。

     inds = len([index for index, item in enumerate(t) if item in ['foo']]) 

在线尝试!


但是,正如您所注意到的,您的代码非常混乱,其他答案显示了如何使用list().index()来更好地发挥作用。

如果您想进一步查找嵌套子列表,则需要进一步说明您希望如何执行此操作。 考虑以下问题:

  • 下载['foo', 'bar'] ,然后['test', 'world']

    • 是否应该仅在初始列表或子列表内部进行子列表?
  • 下载['foo', 'world'] ,然后['test', 'bar']

    • 如何在列表的不同级别上进行匹配?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM