我做了一个函数,它将在字典中查找年龄并显示匹配的名字:
dictionary = {'george' : 16, 'amber' : 19}
search_age = raw_input("Provide age")
for age in dictionary.values():
if age == search_age:
name = dictionary[age]
print name
我知道如何比较和查找年龄,只是不知道如何显示这个人的名字。此外,由于第5行,我得到了一个KeyError。我知道这是不正确的,但我不知道如何让它向后搜索。
以下是我对这个问题的看法。:)
我刚刚开始学习Python,所以我称之为:
“初学者可以理解的”解决方案。
#Code without comments.
list1 = {'george':16,'amber':19, 'Garry':19}
search_age = raw_input("Provide age: ")
print
search_age = int(search_age)
listByAge = {}
for name, age in list1.items():
if age == search_age:
age = str(age)
results = name + " " +age
print results
age2 = int(age)
listByAge[name] = listByAge.get(name,0)+age2
print
print listByAge
.
#Code with comments.
#I've added another name with the same age to the list.
list1 = {'george':16,'amber':19, 'Garry':19}
#Original code.
search_age = raw_input("Provide age: ")
print
#Because raw_input gives a string, we need to convert it to int,
#so we can search the dictionary list with it.
search_age = int(search_age)
#Here we define another empty dictionary, to store the results in a more
#permanent way.
listByAge = {}
#We use double variable iteration, so we get both the name and age
#on each run of the loop.
for name, age in list1.items():
#Here we check if the User Defined age = the age parameter
#for this run of the loop.
if age == search_age:
#Here we convert Age back to string, because we will concatenate it
#with the person's name.
age = str(age)
#Here we concatenate.
results = name + " " +age
#If you want just the names and ages displayed you can delete
#the code after "print results". If you want them stored, don't...
print results
#Here we create a second variable that uses the value of
#the age for the current person in the list.
#For example if "Anna" is "10", age2 = 10,
#integer value which we can use in addition.
age2 = int(age)
#Here we use the method that checks or creates values in dictionaries.
#We create a new entry for each name that matches the User Defined Age
#with default value of 0, and then we add the value from age2.
listByAge[name] = listByAge.get(name,0)+age2
#Here we print the new dictionary with the users with User Defined Age.
print
print listByAge
.
#Results
Running: *\test.py (Thu Jun 06 05:10:02 2013)
Provide age: 19
amber 19
Garry 19
{'amber': 19, 'Garry': 19}
Execution Successful!
这是你访问字典做你想做的事情的方式:
list = {'george': 16, 'amber': 19}
search_age = raw_input("Provide age")
for age in list:
if list[age] == search_age:
print age
当然,你们的名字太离谱了,看起来像是要打印一个年龄,但它确实打印了名字。因为你是通过名字来访问的,所以如果你这样写会更容易理解:
list = {'george': 16, 'amber': 19}
search_age = raw_input("Provide age")
for name in list:
if list[name] == search_age:
print name
更好的是:
people = {'george': {'age': 16}, 'amber': {'age': 19}}
search_age = raw_input("Provide age")
for name in people:
if people[name]['age'] == search_age:
print name
在我的情况下,最简单的方法是实例化字典在你的代码,然后你可以从它调用键如下
这是我们班有字典
class Config:
def local(self):
return {
"temp_dir": "/tmp/dirtest/",
"devops": "Mansur",
}
实例化你的字典
config = vars.Config()
local_config = config.local()
最后调用你的字典键
patched = local_config.get("devops")
考虑使用Pandas。正如William McKinney的《Python for Data Analysis》中所述
另一种考虑级数的方法是固定长度的有序级数
Dict,因为它是索引值到数据值的映射。它可以是
在很多情况下,你可能会用到字典。
import pandas as pd
list = {'george':16,'amber':19}
lookup_list = pd.Series(list)
要查询您的系列,请执行以下操作:
lookup_list[lookup_list.values == 19]
收益率:
Out[1]:
amber 19
dtype: int64
如果您需要对输出进行任何其他转换
回答成一个列表可能有用:
answer = lookup_list[lookup_list.values == 19].index
answer = pd.Index.tolist(answer)