我有一个字符串列表,我想将所有标记提取到一组标记中,而不是一组标记。我需要把所有的信物都弄混。你知道吗
我的句子存储为“句子”中的字符串列表
所以如果你尝试:
words = set([])
a=set(sentences[1].split())
b=set(sentences[2].split())
a.union(b)
我把a和b放在这样一组中。这就是我要找的
{',', '.', '2.252', '35-1/7', '37-year-old', 'B', 'Blood', 'Fred', 'G4', 'Grauman', 'O+', 'P3-5', 'pregnancy', 'product', 'rubella', surface', 'the', 'to', 'type', 'week', 'woman'}
但是有了列表理解
words = set()
[words.union(set(sent.split())) for sent in sentences]
输出是一个集合列表,如下所示
[{'.', 'Care', 'He', 'Intensive', 'Neonatal''}, {'.', '2.252', 35-1/7', '37-year-old', 'Fred', 'G4', 'Grauman','}]
有没有办法用一些紧凑的代码行(比如列表)来获得我需要的东西?你知道吗
======
好吧,我刚刚做了,在“单词”列表理解之后
a = set()
a.union(*words)
谢谢
如果你的句子是串在一起的,你可以把它们连在一起,然后再把它们分开。你知道吗
转
['A short sentence', 'A second sentence']
变成{'A', 'second', 'sentence', 'short'}
做些什么
或者你可以试着从functools中使用reduce。你知道吗
相关问题 更多 >
编程相关推荐