如何在不包含括号、逗号和引号的情况下拆分列表

问题描述 投票:0回答:1

enter image description here

当我拆分列表时,单词旁边的逗号、引号和方括号也会随之拆分。我怎样才能获取我的句子列表,并将它们分成没有上述字符的单词?

想要单独的单词,在我的单词后面附上逗号、引号和括号

list split
1个回答
0
投票

在第 2 行中,您将整个列表转换为字符串,包括逗号和引号。相反,我会尝试将它们全部放入一个字符串中,其中包含所有句子中的单词,但没有引号或逗号。我会做这样的事情来获得“strsent”:

# declare your sentences list here
strsent = ' '.join(sentences)
split = strsent.split()
# the rest of your code after line 4 would be the same

如果你真的想简化所有代码,你可以这样做:

sentences = [] # add your own sentences here
strsent = ' '.join(sentences)
split = strsent.split()
unique_words = sorted(set(split))
print(unique_words)

如果这有帮助或者您还有其他问题,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.