提问人:LaLuna 提问时间:11/7/2023 更新时间:11/7/2023 访问量:59
如何比较几个列表[],并打印具有匹配项的列表
How to Compare a few Lists[], and print the Lists that have matching items
问:
基本上,我试图根据关键字 input() 从我最喜欢的电影列表中创建一个随机电影选择器。
我想搜索“喜剧”,在文本框中取回喜剧标题列表,以及一个随机选择的“喜剧标题”配置到标签中。
我的方法是......
list= [["Title1", "Comedy", "Movie"],
["Title2", "Comedy", "Movie"],
["Title3", "Anime", "Movie"],
["Title4", "Horror", "Show"],
["Title5", "Comedy", "Show"],
["Title6", "Comedy", "Show"],
["Title7", "Horror", "Movie"],
["Title8", "Comedy", "Movie"],
["Title9", "Comedy", "Show"],
["Title10", "Anime", "Show"]]
def picker_button():
for item in list:
if item[1] == textBox1.get():
textBox2.insert(customtkinter.END, item[0] + "\n")
label.configure(text=random.choice(item[0]))
目标
- 给出 “Comedy” 或 “Show” 的 input()。
- 根据命令,遍历列表,在每个列表中查找输入(关键字)。
- insert() 将找到的“标题”列表添加到 textBox 中。
- 并将标签配置为喜剧标题列表中的 “title”/item[0] 的随机选择。
结果
- 接受 input() 很好。
- 循环无误。
- 成功,将 “Comedy” 相关 “titles”/item[0] 列表插入 textBox。
- 无法获取“标题”的随机选择。标签 配置为仅最后一个列表/“title10” 中的 item[0]/“title” 的单个字符。当input() == “Comedy”时,它将标签更改为:“t”或“i”或“e”或“1” --GUI输出的屏幕截图
问题:
- 我怎样才能让我的标签configure()到满足“喜剧”过滤器的“扫描列表”的“标题”/item[0]的随机选择。
- 并返回整个“标题”,而不仅仅是“t”或“i???
以下是实际代码:
from random import choice
import customtkinter
from tkinter import *
import random
from PIL import Image, ImageTk
import tkinter
# theme and color
customtkinter.set_appearance_mode("dark") #"system", "light", or "dark"
customtkinter.set_default_color_theme("dark-blue") #"blue", "dark-blue", or "green"
#root = Tk()
root = customtkinter.CTk()
root.geometry("600x600")
root.iconbitmap('images/codemy.ico')
root.title("Bucket Pull")
img1=ImageTk.PhotoImage(Image.open("sasa.jpg"), Image.Resampling.LANCZOS)
World=customtkinter.CTkLabel(master=root, image=img1, corner_radius=15)
World.pack(pady=20, padx=20)
Logframe = customtkinter.CTkFrame(master=World, width=320, height=360)
Logframe.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
# Movies
movies = [["Hitchhikers Guide to the Galaxy", "Comedy", "Movie"],
["Monty Python: Life of Brian", "Comedy", "Movie"],
["Nothing But Trouble", "Comedy", "Movie"],
["Planet Terror", "Action", "Movie"],
["Futurama", "Comedy", "Show"],
["Death Proof", "Suspense", "Movie"],
["The Machine", "Comedy", "Movie"],
["Spirited Away", "Anime", "Movie"],
["I Think You Should Leave!", "Comedy", "Show"],
["Kill Tony", "Comedy", "Show"],
["Seinfeld", "Comedy", "Show"],
["Barbie", "Comedy", "Movie"],
["Back to the Future", "Comedy", "Movie"],
["South Park", "Comedy", "Show"],
["Fruits Basket", "Anime", "Show"]]
def moodie():
for item in movies:
if item[1] == My_Mood.get():
Mood_text.insert(customtkinter.END, item[0] + "\n")
Mood_Label.configure(text=random.choice(item))
def clear():
Mood_Label.configure()
Main_Label = customtkinter.CTkLabel(master=Logframe,text=" Bucket Pull ",font=("klee", 45),text_color="lightblue")
Main_Label.pack(pady=20, padx=20)
#Entry
My_Mood = customtkinter.CTkEntry(master=Logframe, width=200, height=40)
My_Mood.pack(pady=20, padx=20)
#Button
Mood_Button = customtkinter.CTkButton(master=Logframe, text="Mood", width=60, height=20, command=moodie)
Mood_Button.pack(pady=10, padx=10)
#Label
Mood_Label = customtkinter.CTkLabel(master=Logframe, text="A", font=("muna", 18),fg_color="transparent")
Mood_Label.pack(pady=20, padx=20)
#Textbox
Mood_text = customtkinter.CTkTextbox(master=Logframe, height=200, width=250)
Mood_text.pack(pady=20, padx=20)
#ADD Ween Button
root.mainloop()
答: 暂无答案
下一个:zip 循环未按预期运行
评论
random.choice(item[0])
正在做出一个随机选择,从中选择一个字符串......听起来您想从已验证的项目(具有流派字符串匹配)中随机选择。为此,您需要从第二个过滤列表中进行选择。为此,您可以使用列表推导式,或者从返回的字符串将具有换行符的文本对象中,您可以将其用作锚点,将文本拆分为列表。item[0]
.get