如何在Python中读取循环中的多个.txt文件并获得匹配单词的计数

如何在Python中读取循环中的多个.txt文件并获得匹配单词的计数,python,Python,我有两个文本文件和两个列表(第一个列表,第二个列表),我想分别从第一个列表和第二个列表中找出每个文件匹配单词的数量 FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize" SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing" (这不是一个字符串,我正在获取此数据的“.txt”文件格式) 文本文件1(包含): 文本文件

我有两个文本文件和两个列表(第一个列表,第二个列表),我想分别从第一个列表和第二个列表中找出每个文件匹配单词的数量

FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize"
SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing"
(这不是一个字符串,我正在获取此数据的“.txt”文件格式)

文本文件1(包含):

文本文件2(包含):

输出格式:

File1 first list count=2
File1 second list count=0

File2 first list count=0
File2 second list count=4
下面的代码我已经尝试过归档这个功能,但无法获得预期的输出。 如果有任何帮助,我们将不胜感激

读取所有(x.txt文件)

创建def函数以删除标点符号

# remove Punctuations
import re

def remove_punctuation(line):
return re.sub(r'[^\w\s]', '', line)
从循环中的“文件名”读取多个文件,但它正在合并。我需要分开每个text1文件计数和text2文件计数

two_files=[]
for filename in files:
    for line in open(filename):
        #two_files.append(remove_punctuation(line))
        print(remove_punctuation(line),end='')
        two_files.append(remove_punctuation(line))


FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize"
SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing"


c=[]
for match in FIRST_LIST:
    if any(match in value for value in two_files):
        #c=match+1
        print (match)
        c.append(match)
print(c)
len(c)
d=[]
for match in SECOND_LIST:
    if any(match in value for value in two_files):
        #c=match+1
        print (match)
        d.append(match)
print(d)
len(d)

我不确定这是否是您想要的,但我认为这是因为您将两个文件中的行追加到同一列表中。您应该为每一项创建一个列表。尝试:

import glob
files=[]

for filename in glob.glob("*.txt"):
    files.append(filename)

# remove Punctuations
import re

def remove_punctuation(line):
    return re.sub(r'[^\w\s]', '', line)


two_files=[]
for filename in files:
    temp = []
    for line in open(filename):
        temp.append(remove_punctuation(line))
    two_files.append(temp)

FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize"
SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing"


c=[]
d=[]

for file in two_files:
    temp = []
    for match in FIRST_LIST:
        for value in file:
            if match in value:
                temp.append(match)
    c.append(temp)

    temp2 = []
    for match in SECOND_LIST:
        for value in file:
            if match in value:
                temp2.append(match)
    d.append(temp2)

print('File1 first list count = ' + str(len(c[0])))
print('File1 second list count = ' + str(len(d[0])))

print('File2 first list count = ' + str(len(c[1])))
print('File2 second list count = ' + str(len(d[1])))

它存储TextFile_1,但不循环TextFile_2。我需要一个动态问题,以确保我理解您真正想要的内容。由于LIST1和LIST2是相同的,但单词的顺序不同,所以两个列表中文本文件1的匹配计数不应该为2,而列表1的匹配计数不应该为2,列表2的匹配计数不应该为0?第一个列表是英国英语,第二个列表是美国英语。它们的书写方式完全相同。我根据我之前的答案添加了代码的其余部分,您的代码的其余部分也可以使用,但是第一个列表的计数数与第二个列表的计数数相同。不管怎样,我希望我帮助过你。
# remove Punctuations
import re

def remove_punctuation(line):
return re.sub(r'[^\w\s]', '', line)
two_files=[]
for filename in files:
    for line in open(filename):
        #two_files.append(remove_punctuation(line))
        print(remove_punctuation(line),end='')
        two_files.append(remove_punctuation(line))


FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize"
SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing"


c=[]
for match in FIRST_LIST:
    if any(match in value for value in two_files):
        #c=match+1
        print (match)
        c.append(match)
print(c)
len(c)
d=[]
for match in SECOND_LIST:
    if any(match in value for value in two_files):
        #c=match+1
        print (match)
        d.append(match)
print(d)
len(d)
import glob
files=[]

for filename in glob.glob("*.txt"):
    files.append(filename)

# remove Punctuations
import re

def remove_punctuation(line):
    return re.sub(r'[^\w\s]', '', line)


two_files=[]
for filename in files:
    temp = []
    for line in open(filename):
        temp.append(remove_punctuation(line))
    two_files.append(temp)

FIRST_LIST = "accessorizes","accessorizing","accessorized","accessorize"
SECOND_LIST="accessorize","accessorized","accessorizes","accessorizing"


c=[]
d=[]

for file in two_files:
    temp = []
    for match in FIRST_LIST:
        for value in file:
            if match in value:
                temp.append(match)
    c.append(temp)

    temp2 = []
    for match in SECOND_LIST:
        for value in file:
            if match in value:
                temp2.append(match)
    d.append(temp2)

print('File1 first list count = ' + str(len(c[0])))
print('File1 second list count = ' + str(len(d[0])))

print('File2 first list count = ' + str(len(c[1])))
print('File2 second list count = ' + str(len(d[1])))