简体   繁体   English

如何在 For 循环 Python 中按字典中的键获取值

[英]How to get Values by Keys in Dictionary in For loop Python

I have got this code:我有这个代码:

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
print(mylist[char])

and it works fine, but when I change it to this:它工作正常,但是当我将其更改为:

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for char in mylist:
    if mylist[char] == char:
        print(mylist[char])

it dos not return any value, neither gives error message.它不返回任何值,也不给出错误消息。

What is missing or wrong?有什么遗漏或错误?

Thank you!谢谢!

BR, Valters BR,瓦尔特斯

First, don't call 2 things the same here char .首先,不要在这里char称两件事相同。 As you iterate the dict you get the keys and it's with these keys that you need to check for equality当您迭代dict时,您会获得keys ,并且您需要使用这些密钥检查是否相等

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for key in mylist:
    if key == char:
        print(mylist[key])

But doing that make the all purpose of using a dict disappear, you loose the performance of just doing mylist[char]但是这样做会使使用dict的所有目的消失,你失去了只做mylist[char]的性能

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist.keys():
  print mylist.get(char, None)

In your code you are actually trying to compare your key with dictionary value, and there it fails.在您的代码中,您实际上是在尝试将您的键与字典值进行比较,但它失败了。

I hope I answered your problem.我希望我回答了你的问题。

The char in the loop scoop is referring to a key of the mylist so when you are doing in the loop if mylist[char] == char it's checking if the key is equal to the value.循环勺中的 char 指的是 mylist 的键,因此当您在循环中执行if mylist[char] == char时,它会检查键是否等于值。
This is how it looks like inside the loop on each iteration.这就是每次迭代在循环内部的样子。

mylist[char] 1 char a
mylist[char] 2 char b
mylist[char] 3 char c

so in the second example, it's not getting into the if statement that will create you a list of all lines that you can iterate over it over and over again.所以在第二个例子中,它没有进入 if 语句,它会为你创建一个所有行的列表,你可以一遍又一遍地对其进行迭代。
You can do你可以做

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist:
    print(mylist[char])
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')

for char in mylist:
   if mylist[char] == char:
      print(mylist[char])

The problem with this is, when you are looping through a dictionary, you are checking if key==value which returns false and so nothing gets printed.这样做的问题是,当您遍历字典时,您正在检查是否返回 false 的key==value ,因此没有打印任何内容。

for char in mylist Here the char value is the keys so for your approach you have to modify the if condition. for char in mylist这里 char 值是键,因此对于您的方法,您必须修改if条件。 Instead of checking keys with value check keys with the entered chars.而不是使用输入的字符检查键值检查键。

So this will be like:所以这将是这样的:

for key in mylist:
   if key == char:
      print(mylist[char])

And also instead of using loop and if condition, you can simply do as below:而且,您可以简单地执行以下操作,而不是使用循环和 if 条件:

if char in mylist:  # it checks if char is present in dict's key or not
   print(mylist[char])

yes it wont work the reason is是的,它不起作用,原因是

mylist = {'a':1,'b':2,'c':3} mylist = {'a':1,'b':2,'c':3}

char = input('Enter char: ') // <- here you are assigning the user input to the variable char. char = input('Enter char: ') // <- 在这里您将用户输入分配给变量 char。

for char in mylist: // <- the variable char is again assigned different value ie the keys from the dictionary for char in mylist: // <- 变量 char 再次被赋予不同的值,即字典中的键

if mylist[char] == char: // <- so here in first iteration the char will be "a" in " mylist[char] ", which is compared with char "a", so the condition is as follows. if mylist[char] == char: // <- 所以这里在第一次迭代中 char 将是 "mylist[char] " 中的 "a",它与 char "a" 进行比较,所以条件如下。

mylist["a"] == a // <- but here the value will be " 1 == a " since this condition fails mylist["a"] == a // <- 但是这里的值将是“ 1 == a ”,因为这个条件不成立

print(mylist[char]) // <- the print statement is not executed. print(mylist[char]) // <- 不执行打印语句。

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

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