简体   繁体   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()

i have two separate lists containing strings in it.one is bigger in size while other one is smaller in size, i want to check whether strings which are present in smaller strings of lists are present in longer or not here is some example below please help me out to find solution for it longer list: '['how', 'many', 'people', 'are', 'there', 'in', 'your', 'family', 'there', 'are', 'five', 'people', 'in', 'my', 'family', 'my', 'father', 'mother', 'brother', 'sister', 'and', 'me', 'house', 'in', 'the', 'countryside']' smaller list: ['people', 'in', 'my', 'family']我有两个单独的列表,其中包含字符串。一个较大,而另一个较小,我想检查较小的列表字符串中存在的字符串是否存在更长的时间这里是下面的一些示例,请帮助我出去寻找解决方案更长的列表:'['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']

You can use the issubset method of set您可以使用setissubset方法

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

This checks if all the elements present in the smaller_list are present in the larger_list as well.这将检查larger_list smaller_list In short, it checks if smaller_list is a subset of larger_list简而言之,它检查larger_list smaller_list子集

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

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