简体   繁体   English

如何创建多值字典?

[英]How can I create a multi-value dictionary?

I have user info from a SQL Server - a username and password - and now I want to create a dictionary我有来自 SQL Server 的用户信息 - 用户名和密码 - 现在我想创建一个字典

{ID : password}

but if I have more then one it just saves the last one I put in但如果我有更多然后一个它只会保存我放入的最后一个

for i in (cursor.execute('select * from Person')):
    idNameDict = {i[0]:i[3]}

I want to do it this way so it would be easier for me to do check if the based on the ID that the password input would be correct because right now and it seems to long我想这样做,这样我就可以更轻松地检查基于 ID 的密码输入是否正确,因为现在似乎很长

def logIn(userName,userPassword):
    userID=[]
    global p
    lbl = Label(root)
    lbl.grid(row=5,column=1)
    for user in (cursor.execute('select * from Person')):
        p = Person(user[0],user[1],user[2],user[3])
        userID.append(p)
    for id in userID:
        if userName == id.ID:
            if userPassword == id.password:
                userPage(root,userName)
            else:
                lbl.config(text= "Invalid Password")
        else:
            lbl.config(text= "Invalid User Name")

Your current code doesn't work properly because you declare the dictionary from scratch in each iteration.您当前的代码无法正常工作,因为您在每次迭代中都从头开始声明字典。 You need to do it in the following way:您需要通过以下方式进行操作:

idNameDict = {}
for i in (cursor.execute('select * from Person')):
    idNameDict[i[0]] = i[3]

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

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