简体   繁体   English

仅打印字典中键的值

[英]Printing only the value of a key in a dictionary

I would like further explanation as to why the following code prints the value of the key.我想进一步解释为什么以下代码会打印键的值。 I am learning Python for a class and Zybooks is terrible at explaining.我正在为 class 学习 Python,而 Zybooks 很难解释。

# Complete the function to return a dictionary value
# if it exists or return Not Found if it doesn't exist
def findDictItem(mydict, key):
# Student code goes here
    if key in mydict:
        return (mydict[key])
    else:
        return 'Not Found'
# expected output: yellow
print(findDictItem({'tomato': 'red', 'banana': 'yellow', 'lime': 'green'} , 'banana'))
# expected output: Not Found
print(findDictItem({'Brazil': 'Brasilia', 'Ireland': 'Dublin', 'Indonesia': 'Jakarta'},'Cameroon'))

The return(mydict[key]) , in my understanding, should return the key not the value, but returns they value instead.在我的理解中, return(mydict[key])应该返回键而不是值,而是返回它们的值。

Would someone please provide clarity?有人可以提供清楚吗?

If you want to print only the key or value, you can use something like this.如果你只想打印键或值,你可以使用这样的东西。

a = {'tomato': 'red', 'banana': 'yellow', 'lime': 'green'}
for key,value in a:
    print (key) #will print keys : tomato, banana, lime
    print (value) #will print values : red, yellow, green
    print (a[key]) #will print values : red, yellow, green

If you want keys, just change your code to say如果你想要钥匙,只需改变你的代码说

if key in mydict:
    return key
else:
    return 'Not Found'

Also, you can simplify the code further.此外,您可以进一步简化代码。

return (key) if key in mydict else 'Not Found'

I rewrote your code as follows:我重写了你的代码如下:

def findDictItem(mydict, key):
# Student code goes here
    return key if key in mydict else 'Not Found'
# expected output: yellow
print(findDictItem({'tomato': 'red', 'banana': 'yellow', 'lime': 'green'} , 'banana'))
# expected output: Not Found
print(findDictItem({'Brazil': 'Brasilia', 'Ireland': 'Dublin', 'Indonesia': 'Jakarta'},'Cameroon'))

Output: Output:

banana
Not Found

Just to make an example, there is the dictionary function get which returns a default value if the key isn't in the dictionary.举个例子,如果键不在字典中,字典 function get返回默认值。

# Complete the function to return a dictionary value
# if it exists or return Not Found if it doesn't exist

def findDictItem(mydict, key):
    return mydict.get(key, 'Not found')

Remember that a dictionary is a map of keys to values, like请记住,字典是键值的 map,例如

{
   'Ireland':   'Dublin',
   'Indonesia': 'Jakarta'
#   ^key         ^value
}

mydict[key] returns the value of the dictionary at index key . mydict[key]返回索引key处的字典值。 If you want the value of the dict at a certain key, you would use mydict[key] , so mydict['Ireland'] will return Dublin .如果您想要某个键的 dict 值,您将使用mydict[key] ,因此mydict['Ireland']将返回Dublin

If you just want to return Dublin , you would use return key如果您只想返回Dublin ,则可以使用return key

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

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