Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于列表项内容(字符串)拆分列表_Python_Nested Lists - Fatal编程技术网

Python 基于列表项内容(字符串)拆分列表

Python 基于列表项内容(字符串)拆分列表,python,nested-lists,Python,Nested Lists,我有一个包含大学授课时间和主题的列表 [‘第9周(2015年9月28日)MA4/PGDE/BMU’、‘0900-1000 MA4/PGDE讲座备考’、‘1100-1200 PS教程第1-9组’、‘1300-1400 PS讲座备考’、‘1500-1600 PS讲座备考’、‘第10周……等等 总共有44周,我如何使用“Week”字符串作为触发器将列表拆分为子列表,为每周的讲座提供子列表?如下所示 [“第9周(2015年9月28日)MA4/PGDE/BMU”,“0900-1000 MA4/PGDE讲座

我有一个包含大学授课时间和主题的列表

[‘第9周(2015年9月28日)MA4/PGDE/BMU’、‘0900-1000 MA4/PGDE讲座备考’、‘1100-1200 PS教程第1-9组’、‘1300-1400 PS讲座备考’、‘1500-1600 PS讲座备考’、‘第10周……等等

总共有44周,我如何使用“Week”字符串作为触发器将列表拆分为子列表,为每周的讲座提供子列表?如下所示

[“第9周(2015年9月28日)MA4/PGDE/BMU”,“0900-1000 MA4/PGDE讲座备考”,“1100-1200 PS教程组仅限1-9”,“1300-1400 PS讲座备考”,“1500-1600 PS讲座备考”,“第10周…等等…”

我没有任何代码…这就是为什么我要问我是否可以以及如何才能做到这一点!

给定:

li=['Week 9 (28/09/15) MA4/PGDE/ BMus','0900-1000 MA4/PGDE Lecture ALT ', '1100-1200 PS Tutorials Groups 1-9 ONLY ','1300-1400 PS Lecture ALT', '1500-1600 PS Lecture ALT ', 'Week 10... ', 'more on 10', 'and more', 'Week 11... ', 'more on 11', 'and more',]
您可以像这样使用
groupby

from itertools import groupby

result=[]
temp=[]
for k, g in groupby(li, key=lambda s: s.lower().startswith('week')):
    if k:
        if temp:
            result.append(temp)
        temp=list(g)
    else:
        temp.extend(list(g))
else:
    result.append(temp)
>>> results
[['Week 9 (28/09/15) MA4/PGDE/ BMus', '0900-1000 MA4/PGDE Lecture ALT ', '1100-1200 PS Tutorials Groups 1-9 ONLY ', '1300-1400 PS Lecture ALT', '1500-1600 PS Lecture ALT '], ['Week 10... ', 'more on 10', 'and more'], ['Week 11... ', 'more on 11', 'and more']]
您还可以像这样进行切片和压缩(在同一列表中):


其中allWeeks是您的起始数组,array是array,它由数组组成,从element Week开始。

请包括一个最小的、完整的、可验证的示例以及您迄今为止尝试过的内容。堆栈溢出不是一项代码编写服务!请显示您尝试过的代码和不起作用的代码。请参阅如何创建迷你代码错误的,完整的,可验证的,例子。
>>> idxs=[i for i, e in enumerate(li) if s.lower().startswith('week')]+[len(li)]
>>> [li[x:y] for x, y in zip(idxs, idxs[1:])]
[['Week 9 (28/09/15) MA4/PGDE/ BMus', '0900-1000 MA4/PGDE Lecture ALT ', '1100-1200 PS Tutorials Groups 1-9 ONLY ', '1300-1400 PS Lecture ALT', '1500-1600 PS Lecture ALT '], ['Week 10... ', 'more on 10', 'and more'], ['Week 11... ', 'more on 11', 'and more']]
allWeeks = []
arranged = []

current = []
for i in range(len(allWeeks)):

    if ("Week" in allWeeks[i]): 
        arranged.append(current)
        current = []
        current.append(allWeeks[i])
    elif (i == len(allWeeks) - 1):
        current.append(allWeeks[i])
        arranged.append(current)
    else:
        current.append(allWeeks[i])

for i in arranged:
    print (i)