我如何找到所有唯一的单词(没有重复)?
How do I find all unique words(no duplicates)?
我想找到两个文件中的所有唯一单词。我能够列出每个文件中的所有单词,但它给了我重复项。我还想按字母顺序对它们进行排序。我该怎么做?
#!/usr/bin/python3
#First file
file = raw_input("Please enter the name of the first file: ")
store = open(file)
new = store.read()
#Second file
file2 = raw_input("Please enter the name of the second file: ")
store2 = open(file2)
new2 = store2.read()
for line in new.split():
if line in new2:
print line
下面是一个可能对您有帮助的片段:
new = 'this is a bunch of words'
new2 = 'this is another bunch of words'
unique_words = set(new.split())
unique_words.update(new2.split())
sorted_unique_words = sorted(list(unique_words))
print('\n'.join(sorted_unique_words))
更新:
如果您只对两个文件共有的词感兴趣,请改为执行此操作:
unique_words = set(new.split())
unique_words2 = set(new2.split())
common_words = set.intersection(unique_words, unique_words2)
print('\n'.join(sorted(common_words)))
我想找到两个文件中的所有唯一单词。我能够列出每个文件中的所有单词,但它给了我重复项。我还想按字母顺序对它们进行排序。我该怎么做?
#!/usr/bin/python3
#First file
file = raw_input("Please enter the name of the first file: ")
store = open(file)
new = store.read()
#Second file
file2 = raw_input("Please enter the name of the second file: ")
store2 = open(file2)
new2 = store2.read()
for line in new.split():
if line in new2:
print line
下面是一个可能对您有帮助的片段:
new = 'this is a bunch of words'
new2 = 'this is another bunch of words'
unique_words = set(new.split())
unique_words.update(new2.split())
sorted_unique_words = sorted(list(unique_words))
print('\n'.join(sorted_unique_words))
更新:
如果您只对两个文件共有的词感兴趣,请改为执行此操作:
unique_words = set(new.split())
unique_words2 = set(new2.split())
common_words = set.intersection(unique_words, unique_words2)
print('\n'.join(sorted(common_words)))