简体   繁体   English

我想要两个列表框来显示彼此对应的项目

[英]I want two list boxes to show items that correspond to each other

I have two list boxes, one has words, and the other will have numbers.我有两个列表框,一个有文字,另一个有数字。 In the code I am using as an example, I simplified it so that the first list box has a list of letters going from a to j and then repeats this list, but with a number next to those letters.在我用作示例的代码中,我对其进行了简化,以便第一个列表框具有从 a 到 j 的字母列表,然后重复此列表,但这些字母旁边有一个数字。 The second list box has just numbers.第二个列表框只有数字。 Now I want to be able to type 'a' and it show the items in list 1 that start with 'a', and then in the second list box show the numbers that correspond to the items in list box 1. For example, a should have 1 correspond to it, b should have 2, and so on.现在我希望能够键入“a”,它会显示列表 1 中以“a”开头的项目,然后在第二个列表框中显示与列表框 1 中的项目相对应的数字。例如,a应该有1对应,b应该有2,依此类推。 I have already figured out how to make the first list box show all the items that start with the letter I type, but I have no idea how to make the second list box show the numbers that go with those items.我已经想出如何让第一个列表框显示所有以我键入的字母开头的项目,但我不知道如何让第二个列表框显示 go 与这些项目的数字。

from tkinter import *

win = Tk()
win.title("test")
win.geometry("1000x600")
win.resizable(False, False)

dummylist2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18'
    , '19', '20']
dummylist1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'a2', 'b1', 'c5', 'd3', 'e8', 'f9', 'g10', 'h4'
    , 'i6', 'j7']


searchcounter = 0
entercounter = 0


def characterpressedfunction(e):  # sets up the function of the b button when it is pressed
    nlist = list(filter(lambda x: x.lower().startswith(nsearchentry.get()),
                        dummylist1))  # removes all words that don't start with the
    #  character that the user typed in
    alphabetlist.delete(0, END)  # clears the alphabetlist
    for item in nlist:  # loops though the list
        alphabetlist.insert(END, item)  # moves each individual item from the list into the listbox
        # as the list gets looped


alphabetlist = Listbox(win, width=20, font=('Arial', 15))
numberlist = Listbox(win, width=20, font=('Arial', 15))

alphabetlist.delete(0, END)  # clears the alphabetlist
for item in dummylist1:  # loops though the list
    alphabetlist.insert(END, item)  # moves each individual item from the list into the listbox
    # as the list gets looped


numberlist.delete(0, END)  # clears the alphabetlist
for item in dummylist2:  # loops though the list
    numberlist.insert(END, item)  # moves each individual item from the list into the listbox
    # as the list gets looped


nsearchentry = Entry(win, width=2, font=('Arial', 15))
nsearchentry.bind("<KeyRelease>", characterpressedfunction)


numberlist.place(relx=.4, rely=.2)
alphabetlist.place(relx=.1, rely=.2)
nsearchentry.place(relx=.3, rely=.7)

win.mainloop()

You can make nlist a list of tuple (alpha, number) as below:您可以使nlist成为元组 (alpha, number) 的列表,如下所示:

def characterpressedfunction(e):
    srch = nsearchentry.get()
    nlist = list(filter(lambda x: x[0].lower().startswith(srch), zip(dummylist1, dummylist2)))
    alphabetlist.delete(0, END)
    numberlist.delete(0, END)
    for a,n in nlist:  # loops through the filtered list
        alphabetlist.insert(END, a)
        numberlist.insert(END, n)

Note that I prefer using list comprehension instead:请注意,我更喜欢使用列表理解:

srch = nsearchentry.get()
nlist = [(a,n) for a,n in zip(dummylist1, dummylist2) if a.lower().startswith(srch)]

Or even remove the creation of nlist and just use a for loop:或者甚至删除nlist的创建并只使用 for 循环:

def characterpressedfunction(e):
    alphabetlist.delete(0, END)
    numberlist.delete(0, END)
    srch = nsearchentry.get()
    for a,n in zip(dummylist1, dummylist2):
        if a.lower().startswith(srch):
            alphabetlist.insert(END, a)
            numberlist.insert(END, n)

Refer to the official document about zip() .参考官方文档zip()

暂无
暂无

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

相关问题 连接两个彼此对应的列表以创建一个类对象列表 - Joining two lists that correspond to each other to create a list of class objects 返回彼此对应的两个列表? - returning two lists that correspond with each other? 我有一个列表,我想计算列表中每个项目与列表中所有其他项目的平均距离 - I have a list and I want to calculate the average distance of each item in the list to all the other items in the list 我想将整个列表相互附加 - I want to append the whole list to each other 两个列表中唯一项的列表,彼此之间重复 - List of unique items from two lists with duplicates between each other 我希望能够使用两个搜索栏同时更新两个列表框 - I want to be able to update two list boxes at the same time with two searchbars 我想在字典中添加我的两个列表项 - I want to add my two list items in a dictionary 我想迭代两个循环,一个是列表,另一个是 python 中的语句 - I want to iterate two loops one is list and the other is a statement in python 我有一个从另一个列表创建的列表。 但是,我不希望两个名字彼此重复。 我怎样才能做到这一点? - I have a list which has been created from another list. However, I do not want two names repeating after each other. How can I do that? Python:我有一个字符串和一系列不同长度的列表,我想返回组中的字母,因为它们对应于列表 - Python: I have a string and a list of lists of varying lengths and I want to return the letters in groups as they correspond to the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM