我正在尝试处理单词列表并返回一个新列表 仅包含唯一的单词。我的确定循环有效,但它只会将所有单词打印在一起,而不是每行一个。谁能帮我?这可能是一个简单的问题,但我对Python很陌生。谢谢!
uniqueWords = [ ]
for word in allWords:
if word not in uniqueWords:
uniqueWords.append(word)
else:
uniqueWords.remove(word)
return uniqueWords
str.join
:
>>> all_words = ['two', 'two', 'one', 'uno']
>>> print('n'.join(get_unique_words(all_words)))
one
uno
或普通for loop
:
>>> for word in get_unique_words(all_words):
... print(word)
...
one
uno
但是,您的方法不适用于奇数:
>>> get_unique_words(['three', 'three', 'three'])
['three']
如果您的目标是获取只出现一次的所有单词,这里有一个使用 collections.Counter
的较短方法:
from collections import Counter
def get_unique_words(all_words):
return [word for word, count in Counter(all_words).items() if count == 1]
这段代码可能会有所帮助,它逐行打印唯一的单词,这是我在您的问题中理解的:
allWords = ['hola', 'hello', 'distance', 'hello', 'hola', 'yes']
uniqueWords = [ ]
for word in allWords:
if word not in uniqueWords:
uniqueWords.append(word)
else:
uniqueWords.remove(word)
for i in uniqueWords:
print i
如果单词的顺序不重要,我建议您创建一个集合来存储唯一的单词:
uniqueWords = set(allWords)
正如你所看到的,运行下面的代码,它可以快得多,但它可能取决于原始的单词列表:
import timeit
setup="""
word_list = [str(x) for x in range(1000, 2000)]
allWords = []
for word in word_list:
allWords.append(word)
allWords.append(word)
"""
smt1 = "unique = set(allWords)"
smt2 = """
uniqueWords = [ ]
for word in allWords:
if word not in uniqueWords:
uniqueWords.append(word)
else:
uniqueWords.remove(word)
"""
print("SET:", timeit.timeit(smt1, setup, number=1000))
print("LOOP:", timeit.timeit(smt2, setup, number=1000))
输出:
SESET:0.03147706200002176
循环:0.12346845000001849
也许这符合你的想法:
allWords=['hola', 'hello', 'distance', 'hello', 'hola', 'yes']
uniqueWords=dict()
for word in allWords:
if word not in uniqueWords:
uniqueWords.update({word:1})
else:
uniqueWords[word]+=1
for k, v in uniqueWords.items():
if v==1:
print(k)
指纹:
distance
yes