繁体   English   中英

如何制作一个识别 2 个文本文件共有的单词的 Python 程序?

[英]How to make a Python program that recognizes words common to 2 text files?

所以,我正在制作一个 python 程序,它将从 .txt (source.txt) 文件中读取代码,并查看 source.txt 是否包含某个单词列表 (words.txt) 中的任何单词。 另外,我需要它告诉我哪个是常用词。
所以,知道如何做到这一点吗?

文本文件:-

 Hello, How are you today
 I am doing very fine fine
 I am also very cool
 My friends are cool too
 We are all very cool

代码: - 不故意使用任何列表推导式。

index = []      #Empty List
check = ['fine', 'cool']   #Words to check for
with open('Sample', 'r') as file:  #Open Text File
    for line in file:                 #Line in text file
        for word in line.split():      #Split the line into words
            for i in range(len(check)):   #Check if words from check match the words in the line
                if word == check[i]:          #i equals the index of the word in the list "check"
                    index.append(i)            #We add the index to our index list


#Find the most common index in our index list
max = 0
res = index[0]
for i in index:
    freq = index.count(i)
    if freq > max:
        max = freq
        res = i              #The element with this index in "check" is the most common
print("The most common word is :", check[res],"It occurs", max, "times in the file")

输出:

The most common word is : cool It occurs 3 times in the file
  1. 从源 txt 文件中读取,使用正则表达式或拆分从文本文件中获取单词列表。 方法可能会有所不同。

  2. 对你的 words.txt 做同样的事情

  3. 设置和运算符

下面是不好的,但一个有效的例子:

f = open('./source.txt').read()
f2 = open('./words.txt').read()

a = set(' '.join(f.split('\n')).split(' '))
b = set(' '.join(f2.split('\n')).split(' ')) 


print (a&b)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM