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

时间:2019-02-17 13:12:15

标签: python

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

example list:-

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

我必须找到在list2 abstract1,abctract2等中找到多少list1内容

4 个答案:

答案 0 :(得分:0)

您需要遍历list1并在count()中使用list2方法

语法:

list2.count(element)

其中元素将是list1中的元素。

答案 1 :(得分:0)

您可以使用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

答案 2 :(得分:0)

我尝试了以下编码。让我知道这是您问题的预期输出。

Set-AzVMCustomScriptExtension -Location "East US" -VMName "TINFAD01" -Name "TINFAD01DSC" -StorageAccountName "p1caddraassbdevdevdiag" -StorageAccountKey "XXXXXX" -FileName "TINFAD01.ps1" -ContainerName "dscfiles"| Update-AzVM -Verbose

输出

import face_recognition
import cv2
import math


video_capture = cv2.VideoCapture(0)
while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    face_landmarks = face_recognition.face_landmarks(frame)
    try:
        p1=face_landmarks[0]['top_lip']
        p2=face_landmarks[0]['bottom_lip']
        x1,y1=p1[9]
        x3,y3=p1[8]
        x4,y4=p1[10]
        x2,y2=p2[9]
        x5,y5=p2[8]
        x6,y6=p2[10]
        dist = math.sqrt(((x2+x5+x6) - (x1+x3+x4)) ** 2 + ((y2+y5+y6) - (y1+y3+y4)) ** 2)
        print(dist)
        image = cv2.circle(frame, p1[8], 1, (255, 255, 255, 0), 2)
        image = cv2.circle(frame, p1[9], 1, (255, 255, 255, 0), 2)
        image = cv2.circle(frame, p1[10], 1, (255, 255, 255, 0), 2)

        image = cv2.circle(frame, p2[8], 1, (255, 255, 255, 0), 2)
        image = cv2.circle(frame, p2[9], 1, (255, 255, 255, 0), 2)
        image = cv2.circle(frame, p2[10], 1, (255, 255, 255, 0), 2)
        # # cv2.clipLine(frame, p1, p2,(255,255,255,0), thickness=2)
        # for p1t in p1:
        #     image = cv2.circle(frame, p1t, 1, (255,255,255,0), 2)
        # for p1b in p2:
        #     image = cv2.circle(frame, p1b, 1, (255, 255, 255, 0), 2)

        cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
        cv2.imshow('Video', frame)
    except Exception as e:
        raise(e)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break



video_capture.release()
cv2.destroyAllWindows()

答案 3 :(得分:0)

您在寻找这种编码吗?

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)

输出

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