简体   繁体   中英

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']

You can use the issubset method of set

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. In short, it checks if smaller_list is a subset of larger_list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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