如果我们在 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/