Python 在两个列表中查找每个单词的频率

Python 在两个列表中查找每个单词的频率,python,Python,我有两个列表,其中有一些细菌的名字,另一个有研究摘要,我必须找到细菌在摘要列表中的频率 example list:- list1 = ['Bac1','Bac2','Bac3','Bac4','Bac5','Bac'] list2 = ['Abstract1','Abstract2','Abstract3','Abstract4','Abstract5','Abstract6'] 我必须找出在list2 abstract1、abctract2等中找到list1内容的次数您需要迭代list1并

我有两个列表,其中有一些细菌的名字,另一个有研究摘要,我必须找到细菌在摘要列表中的频率

example list:-

list1 = ['Bac1','Bac2','Bac3','Bac4','Bac5','Bac']
list2 = ['Abstract1','Abstract2','Abstract3','Abstract4','Abstract5','Abstract6']

我必须找出在list2 abstract1、abctract2等中找到list1内容的次数

您需要迭代list1并在
list2
中使用
count()
方法

语法:

list2.count(element)
其中元素将是您可以使用的
列表1中的元素


我试过下面的编码。让我知道这是您的问题的预期输出

print("List1 is : ",list1)
print("List2 is : ",list2)
for list_1 in list1:
    c=0
    for list_2 in list2:
        if list_1 is list_2:
            c+=1
    if c>0:
        print(list_1 ," frequency is : " ,c)
输出

List1 is :  ['Bac1', 'Bac2', 'Bac3', 'Bac4', 'Bac5', 'Bac']
List2 is :  ['Abstract1', 'Bac5', 'Bac3', 'Abstract4', 'Bac5', 'Abstract6']
Bac3  frequency is :  1
Bac5  frequency is :  2
Frequency of  Bac1  is :  1
Frequency of  Bac2  is :  1
Frequency of  Bac3  is :  1
Frequency of  Bac4  is :  1
Frequency of  Bac5  is :  1
Frequency of  Bac   is :  6

你在找这个编码吗

import itertools
list1 = ['Bac1','Bac2','Bac3','Bac4','Bac5','Bac']
list2 = ['ABstract1','ABstract2','ABstract3','ABstract4','ABstract5','ABstract6']
n_list=[]
n_list1=[]
start_B=[]
end_c=[]
for s in list2:
    t=list(itertools.permutations(s,4))
    t3=list(itertools.permutations(s,3))
    for i in range(0,len(t)):
        element =''.join(t[i])
        n_list.append(element)
    for i in range(0,len(t3)):
        ele =''.join(t3[i])
        n_list1.append(ele)
for i in n_list:
    if i.startswith('B') and (i.endswith('1') or i.endswith('2') or i.endswith('3') or 
                              i.endswith('4') or i.endswith('5')):
    #if i[0] == 'B':
        start_B.append(i)
for l1_ele in list1:
    c=0
    for n_ele in start_B:
        if l1_ele == n_ele:
            c+=1
    if c!=0:
        print("Frequency of ",l1_ele," is : ",c)
for i in n_list1:
    if i.startswith('B') and (i.endswith('c')):
    #if i[0] == 'B':
        end_c.append(i)
for l1_ele in list1:
    c_3=0
    for n_ele in end_c:
        if l1_ele == n_ele:
            c_3+=1
    if c_3!=0:
        print("Frequency of ",l1_ele,"  is : ",c_3)
输出

List1 is :  ['Bac1', 'Bac2', 'Bac3', 'Bac4', 'Bac5', 'Bac']
List2 is :  ['Abstract1', 'Bac5', 'Bac3', 'Abstract4', 'Bac5', 'Abstract6']
Bac3  frequency is :  1
Bac5  frequency is :  2
Frequency of  Bac1  is :  1
Frequency of  Bac2  is :  1
Frequency of  Bac3  is :  1
Frequency of  Bac4  is :  1
Frequency of  Bac5  is :  1
Frequency of  Bac   is :  6

好的,你的问题是什么?你有一个更有意义的例子吗,这个例子似乎没有这样的交叉点。在列表1中有细菌名称和列表2有研究摘要文本,我猜是
'abstract 1'
'abstract 2'
,等等。这是文章摘要的全文吗?是的,我必须计算列表1(细菌名称)到列表2(研究摘要文本)的频率