简体   繁体   English

在两个列表python中找到每个单词的频率

[英]find the frequency of each word in two list python

I have two lists which have some bacteria name and other have research abstract I have to find the frequency of bacteria in to list of the abstract name list我有两个列表,其中有一些细菌名称,另一个列表有研究摘要,我必须在摘要名称列表的列表中找到细菌的频率

example list:-

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

I have to find how many time list1 content are found in list2 abstract1, abctract2 and so on我必须找出在 list2 abstract1、abctract2 等中找到了多少次 list1 内容

You need to iterate through the list1 and use count() method in list2您需要遍历list1并在list2使用count()方法

Syntax:句法:

list2.count(element)

Where element will be elements from list1 .其中 element 将是list1元素。

You may use Counter .您可以使用Counter

from collections import Counter

cntwords = Counter(list2)
for bacteria in list1:
    print(f"{bacteria}: {cntwords[bacteria]}") #using formatted string literals, available since python3.6

I've tried the below coding.我试过下面的编码。 Let me know that is the expected output for your question.让我知道这是您问题的预期输出。

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)

OUTPUT输出

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

Do you looking for this coding?你在找这个编码吗?

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)

OUTPUT输出

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

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

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