Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Sublist - Fatal编程技术网

Python:包含字符串子列表的列表

Python:包含字符串子列表的列表,python,list,sublist,Python,List,Sublist,我有一个字符串列表s,如下所示: s = ['Hello', 'world', '!', 'How', 'are', 'you', '?', 'Have', 'a', 'good', 'day', '.'] final = [['Hello', 'world', '!'], ['How', 'are', 'you', '?'], ['Have', 'a', 'good', 'day', '.']] 我希望将此列表拆分为子列表。只要有\n新的子列表如下所示:

我有一个字符串列表
s
,如下所示:

s = ['Hello', 'world', '!', 'How', 'are', 'you', '?', 'Have', 'a', 'good', 'day', '.']
final = [['Hello', 'world', '!'],
         ['How', 'are', 'you', '?'],
         ['Have', 'a', 'good', 'day', '.']]
我希望将此列表拆分为子列表。只要有
\n
新的子列表如下所示:

s = ['Hello', 'world', '!', 'How', 'are', 'you', '?', 'Have', 'a', 'good', 'day', '.']
final = [['Hello', 'world', '!'],
         ['How', 'are', 'you', '?'],
         ['Have', 'a', 'good', 'day', '.']]
我试过这个:

x = 0
for i in range(len(s)):
    if s[i] in ('!','?','.','\n'):
         final = s[x: x+i]
    x = i+1

final存储我的输出。没有得到应有的结果。有什么建议吗?

您可以使用以下方法:

s = ['Hello', 'world', '!', 'How', 'are', 'you', '?', 'Have', 'a', 'good', 'day', '.']
letters = ['!', '?', '.']

idxes = [idx for idx, val in enumerate(s) if val in letters]
idxes = [-1] + idxes
answer = [s[idxes[i]+1:idxes[i+1]+1] for i in range(len(idxes[:-1]))]
print(answer)
输出

[['Hello', 'world', '!'], ['How', 'are', 'you', '?'], ['Have', 'a', 'good', 'day', '.']]

这使用内置的
enumerate
函数的列表理解来提取出现标点符号的
s
idx
。然后,它使用另一个列表理解来构造子列表,方法是使用
idx
的值对
s
进行切片

s = ['Hello', 'world', '!', 'How', 'are', 'you', '?', 'Have', 'a', 'good', 'day', '.']
final = []
b = []
for x in s:
    b.append(x)
    if x in ('.', '?', '!', '\n'):
        final.append(b)
        b = []
x=0
final=[]
for i in range(len(s)):
    if s[i] in ('!','?','.','\n'):
        final.append(s[x:i+1])
        x=i+1
只有一点索引问题,并使最终列表收集所有
部分列表。

1让final为空数组

2当不为空且索引 3附加到最后一个数组,0到位置+1个字

4收缩主弦s

5增加索引值

final = []
i =0
while len(s) and i<len(s):
    if s[i] in ('!','?','.','\n'):
         final.append( s[:i+1])
         s  = s[i+1:]
    i +=1  
print(final)
final=[]
i=0

虽然len(s)和i并不经常使用python,但在您的情况下,我认为您也可以尝试从初始列表创建生成器,这样您就不必存储列表列表:

>>> from itertools import chain
>>> def func(s):
...     g = iter(s)
...     def inner_func(g):
...         for x in g:
...             yield x
...             if x in ('.', '?', '!', '\n'):
...                 break
...     while True:
...         try:
...             f = g.next()
...         except StopIteration:
...             break
...         else:
...             yield inner_func(chain([f], g))
>>> [[y for y in x] for x in func(s)]
[['Hello', 'world', '!'], ['How', 'are', 'you', '?'], ['Have', 'a', 'good', 'day', '.']]

我建议使用
enumerate
来获取索引,通常最好使用
set
进行成员资格测试,因为它的时间恒定,而不是元组或数组中的线性搜索list@Copperfield:的确,
enumerate
更适合索引,但会改变代码的结构。我想尽可能靠近原始结构。