Python:如何根据特定元素拆分列表

标签 python list indexing

如果我们在 Python 中有以下列表

sentence = ["I", "am", "good", ".", "I", "like", "you", ".", "we", "are", "not", "friends", "."]

如何拆分它以获得包含以句号结尾的元素的列表?所以我想在我的新列表中获取以下元素:

["I","am","good","."]
["I","like","you","."]
["we","are","not","friends","."]

到目前为止我的尝试:

cleaned_sentence = []
a = 0
while a < len(sentence):
    current_word = sentence[a]
    if current_word == "." and len(cleaned_sentence) == 0:
        cleaned_sentence.append(sentence[0:sentence.index(".")+1])
        a += 1
    elif current_word == "." and len(cleaned_sentence) > 0:
        sub_list = sentence[sentence.index(".")+1:-1]
        sub_list.append(sentence[-1])
        cleaned_sentence.append(sub_list[0:sentence.index(".")+1])
        a += 1
    else:
        a += 1

for each in cleaned_sentence:
    print(each)

sentence 上运行它产生

['I', 'am', 'good', '.']
['I', 'like', 'you', '.']
['I', 'like', 'you', '.']

最佳答案

你可以使用itertools.groupby:

from itertools import groupby
i = (list(g) for _, g in groupby(sentence, key='.'.__ne__))
print([a + b for a, b in zip(i, i)])

这个输出:

[['I', 'am', 'good', '.'], ['I', 'like', 'you', '.'], ['we', 'are', 'not', 'friends', '.']]

如果您的列表并不总是以 '.' 结尾,那么您可以使用 itertools.zip_longest 代替:

sentence = ["I", "am", "good", ".", "I", "like", "you", ".", "we", "are", "not", "friends"]
i = (list(g) for _, g in groupby(sentence, key='.'.__ne__))
print([a + b for a, b in zip_longest(i, i, fillvalue=[])])

这个输出:

[['I', 'am', 'good', '.'], ['I', 'like', 'you', '.'], ['we', 'are', 'not', 'friends']]

关于Python:如何根据特定元素拆分列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590886/

相关文章:

mysql - 结合 BTREE 和地理/空间索引来加速我的查询?

indexing - Neo4j 中可以接受混合类型索引吗?

python - Pytorch 中的缓冲区是什么?

python - 使用 easy_install 在 Python 中安装 rpy2 模块时出错

java - 在我的例子中比较 Set 和 List

java - 是否可以在 java 中修改列表的所有元素?

python - Pandas MultiIndex 级别的自定义分组

python - 如何使paramiko-expect中的tail函数超时?

python - 如何过滤具有带列表的列的 pandas DataFrame?

HTML:列表、子列表和伪元素