[英]Printing an entry in a dictionary in python
I have a dictionary and I am trying to get a code to print using a function. 我有一本字典,正在尝试使用功能打印代码。 I am having trouble getting my function to work and not understanding why it isn't printing the student from my dictionary.
我无法正常使用我的功能,也无法理解为什么它无法从我的词典中打印出学生。
def getstudent(key):
students = {'23A' :['Apple', 'John', 95.6],
'65B' :['Briton', 'Alice', 75.5],
'56C' :['Terling', 'Mary', 98.7],
'68R' :['Templeton', 'Alex', 90.5]}
I want to then run the function and type in getstudent('65B'), but when I run I don't get anything in return. 然后,我想运行该函数并键入getstudent('65B'),但是当我运行时,没有任何回报。
Thanks! 谢谢!
You are not using the key
parameter or returning anything in your function: 您没有使用
key
参数或在函数中返回任何内容:
def getstudent(key):
students = {'23A' :['Apple', 'John', 95.6],
'65B' :['Briton', 'Alice', 75.5],
'56C' :['Terling', 'Mary', 98.7],
'68R' :['Templeton', 'Alex', 90.5]}
return students.get(key) # return
print(getstudent('65B'))
['Briton', 'Alice', 75.5]
Or forget about the function and just access the dict directly with students.get(key)
. 或者忘了该函数,而直接使用
students.get(key)
直接访问字典。
You might also want to output an informative message if the key does not exist which can be done by passing a default value to get: 如果密钥不存在,您可能还希望输出一条提示性消息,这可以通过传递默认值来获得:
students.get(key,"Key does not exist")
Without any error checking: 没有任何错误检查:
def getstudent(key):
students = {'23A' :['Apple', 'John', 95.6],
'65B' :['Briton', 'Alice', 75.5],
'56C' :['Terling', 'Mary', 98.7],
'68R' :['Templeton', 'Alex', 90.5]}
print(students[key])
getstudent('65B')
try like this 这样尝试
def getstudent(key):
students = {'23A' :['Apple', 'John', 95.6],
'65B' :['Briton', 'Alice', 75.5],
'56C' :['Terling', 'Mary', 98.7],
'68R' :['Templeton', 'Alex', 90.5]}
if key in students.keys(): # to handle if key is not present
return students[key]
else: return "No key found"
getstudent('65B')
output: 输出:
['Briton', 'Alice', 75.5]
you need to return the value 您需要返回值
How dictionary works, dictionary has a pair that is called, key
and its value
. 字典的工作方式是,字典有一对称为
key
和其value
。 The value of dictionary can be fetched by its key value. 字典的值可以通过其键值获取。 so what i am doing here.
所以我在这里做什么。 when you call a function with key.
当您使用键调用函数时。 I am fetching the value based on key ie
students[key]
it will give you value of key in your dictionary. 我正在基于密钥获取值,即
students[key]
它将为您提供字典中密钥的值。
students.keys()
will give you list of all key in a dictionary students.keys()
将为您提供字典中所有键的列表
def getstudent(key):
students = {'23A' :['Apple', 'John', 95.6],
'65B' :['Briton', 'Alice', 75.5],
'56C' :['Terling', 'Mary', 98.7],
'68R' :['Templeton', 'Alex', 90.5]}
if key in students:
return students[key]
else:
return "%s is not a valid key" % (key)
If you run getstudent('65B') you will get a list 如果运行getstudent('65B'),您将获得一个列表
['Briton', 'Alice', 75.5] ['Briton','Alice',75.5]
you can then access the list by indexing eg 然后可以通过索引访问列表,例如
a_student = getstudent('65B') # now a_student list is as ['Briton', 'Alice', 75.5]
print a_student[0]
a_student[0] prints 'Briton' a_student [0]打印“布里顿”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.