簡體   English   中英

在 python 中查找 substring 的兩個字符串列表之間的比較

[英]Comparison between two lists of strings for finding substring in python

import tkinter as tk
import speech_recognition as sr
from tkinter import ttk
import tkinter.messagebox
import nltk
from tkinter import *
#--------------1stfile---------------
r = sr.Recognizer()
file = sr.AudioFile("t.wav")
with file as source:
audio = r.record(source, duration=20)
result = r.recognize_google(audio)

with open("audio.txt", "w") as text_file:
  text_file.write("%s" % result)
with open("audio.txt", "r") as file:
  file_content = file.read()
tokens = nltk.word_tokenize(file_content)
print("tokens", tokens)
text_file.close()

def onclick():
 a=1
 tk.messagebox.showinfo("Phrased entered by user : ", phrase_var.get())
 str = phrase_var.get()
 list2 = list(str.split(" "))
 length=len(list2)
 count=0
 for i in tokens:
    for j in list2:
        if(j==i):
            count=count+1
            break
 print(count)
 if(length==count):
    tk.messagebox.showinfo("result", " Phrase " + phrase_var.get() + 
"found in file")


window = tk.Tk()
window.title("Desktop App")
window.geometry("500x500")
window.configure(bg='brown')
ttk.Label(window, text="Write phrase: ").pack()
phrase_var = tk.StringVar()
phrase_entrybox = ttk.Entry(window, width=16, 
textvariable=phrase_var).pack()
ttk.Label(window, text=result).pack()
ttk.Button(window,text="Search", command=onclick).pack()
window.mainloop()

我有兩個單獨的列表,其中包含字符串。一個較大,而另一個較小,我想檢查較小的列表字符串中存在的字符串是否存在更長的時間這里是下面的一些示例,請幫助我出去尋找解決方案更長的列表:'['how', 'many', 'people', 'are', 'there', 'in', 'your', 'family', 'there', 'are '、'五'、'人'、'in'、'my'、'family'、'my'、'father'、'mother'、'brother'、'sister'、'and'、'me'、 'house', 'in', 'the', 'countryside']' 較小的列表:['people', 'in', 'my', 'family']

您可以使用setissubset方法

if set(smaller_list).issubset(set(larger_list)):
    ## Your code

這將檢查larger_list smaller_list 簡而言之,它檢查larger_list smaller_list子集

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM