如何查字典?
How to search through dictionaries?
我是 Python 字典的新手。我正在制作一个简单的程序,它有一个字典,其中包含四个名称作为键,各自的年龄作为值。我想做的是,如果用户输入一个名字,程序会检查它是否在字典中,如果是,它应该显示有关该名字的信息。
这是我目前拥有的:
def main():
people = {
"Austin" : 25,
"Martin" : 30,
"Fred" : 21,
"Saul" : 50,
}
entry = input("Write the name of the person whose age you'd like to know, or write 'ALL' to see all names and ages: ")
if entry == "ALL":
for key, value in people.items():
print ("Name: " + key)
print ("Age: " + str(value) + "\n")
elif people.insert(entry) == True:
print ("It works")
main()
我尝试使用 .index()
搜索字典,因为我知道它在列表中使用,但没有成功。我也尝试检查 this post 但我发现它没有用。
我想知道是否有任何函数可以做到这一点。
足够简单
if entry in people:
print ("Name: " + entry)
print ("Age: " + str(people[entry]) + "\n")
如果您想知道 key
是否是 people
中的键,您可以简单地使用表达式 key in people
,如:
if key in people:
并测试它是否不是people
中的一个键:
if key not in people:
您可以直接引用这些值。例如:
>>> people = {
... "Austun": 25,
... "Martin": 30}
>>> people["Austun"]
或者您可以使用 people.get(<Some Person>, <value if not found>)
.
Python 也支持枚举来遍历字典。
for index, key in enumerate(people):
print index, key, people[key]
你可以这样做:
#!/usr/bin/env python3
people = {
'guilherme': 20,
'spike': 5
}
entry = input("Write the name of the person whose age you'd like to know, or write 'ALL' to see all names and ages: ")
if entry == 'ALL':
for key in people.keys():
print ('Name: {} Age: {}'.format(key, people[key]))
if entry in people:
print ('{} has {} years old.'.format(entry, people[entry]))
else:
# you can to create a new registry or show error warning message here.
print('Not found {}.'.format(entry))
在此处的所有答案中,为什么不呢:
try:
age = people[person_name]
except KeyError:
print('{0} is not in dictionary.'.format(person_name))
测试某些内容是否在 Python 中的字典中的规范方法是尝试访问它并处理失败 -- It is easier to ask for forgiveness than permission (EAFP).
一种可能的解决方案:
people = {"Austin" : 25,"Martin" : 30,"Fred" : 21,"Saul" : 50,}
entry =raw_input ("Write the name of the person whose age you'd like
to know, or write 'ALL' to see all names and ages: ")
if entry == 'ALL':
for key in people.keys():
print(people[key])
else:
if entry in people:
print(people[entry])
我是 Python 字典的新手。我正在制作一个简单的程序,它有一个字典,其中包含四个名称作为键,各自的年龄作为值。我想做的是,如果用户输入一个名字,程序会检查它是否在字典中,如果是,它应该显示有关该名字的信息。
这是我目前拥有的:
def main():
people = {
"Austin" : 25,
"Martin" : 30,
"Fred" : 21,
"Saul" : 50,
}
entry = input("Write the name of the person whose age you'd like to know, or write 'ALL' to see all names and ages: ")
if entry == "ALL":
for key, value in people.items():
print ("Name: " + key)
print ("Age: " + str(value) + "\n")
elif people.insert(entry) == True:
print ("It works")
main()
我尝试使用 .index()
搜索字典,因为我知道它在列表中使用,但没有成功。我也尝试检查 this post 但我发现它没有用。
我想知道是否有任何函数可以做到这一点。
足够简单
if entry in people:
print ("Name: " + entry)
print ("Age: " + str(people[entry]) + "\n")
如果您想知道 key
是否是 people
中的键,您可以简单地使用表达式 key in people
,如:
if key in people:
并测试它是否不是people
中的一个键:
if key not in people:
您可以直接引用这些值。例如:
>>> people = {
... "Austun": 25,
... "Martin": 30}
>>> people["Austun"]
或者您可以使用 people.get(<Some Person>, <value if not found>)
.
Python 也支持枚举来遍历字典。
for index, key in enumerate(people):
print index, key, people[key]
你可以这样做:
#!/usr/bin/env python3
people = {
'guilherme': 20,
'spike': 5
}
entry = input("Write the name of the person whose age you'd like to know, or write 'ALL' to see all names and ages: ")
if entry == 'ALL':
for key in people.keys():
print ('Name: {} Age: {}'.format(key, people[key]))
if entry in people:
print ('{} has {} years old.'.format(entry, people[entry]))
else:
# you can to create a new registry or show error warning message here.
print('Not found {}.'.format(entry))
在此处的所有答案中,为什么不呢:
try:
age = people[person_name]
except KeyError:
print('{0} is not in dictionary.'.format(person_name))
测试某些内容是否在 Python 中的字典中的规范方法是尝试访问它并处理失败 -- It is easier to ask for forgiveness than permission (EAFP).
一种可能的解决方案:
people = {"Austin" : 25,"Martin" : 30,"Fred" : 21,"Saul" : 50,}
entry =raw_input ("Write the name of the person whose age you'd like
to know, or write 'ALL' to see all names and ages: ")
if entry == 'ALL':
for key in people.keys():
print(people[key])
else:
if entry in people:
print(people[entry])