[英]Python - Accessing Multiple Values in list
I am sorry for not sharing detailed information, see below code *很抱歉没有分享详细信息,请参阅下面的代码 *
import json
import requests
def funcname():
response = requests.get('https:aig.com/e/id')
parsed_response = json.loads(response.text)
print(parsed_response)
print(type(parsed_response))
If I run the above the code, I get the following output:如果我运行上面的代码,我会得到以下输出:
{'values': [{'id': '123456','name': 'person1'}, {'id': '-1123678','name': 'person2'},{'id': '566','name': 'person3'}, {'id': '-1123678','name': 'person4'},{'id': '-1123678','name': 'person5'}]}
<class 'dict'>
There are actually 100+ entries, I just listed these 5实际上有100多个条目,我只列出了这5个
I would like to create a python program to ask for input for person name and print the id based on name we give.我想创建一个 python 程序来要求输入人名并根据我们提供的名称打印 id。
So, suppose if I prompted the user for a name
of a person and user input person3
, the output should be the id 566
所以,假设如果我提示用户输入一个人的
name
和用户输入person3
,输出应该是 id 566
Unfortunately, the provided code is throwing an error.不幸的是,提供的代码抛出了错误。
You can construct a dictionary and index into it based on your input:您可以根据您的输入构造一个字典和索引:
>>> myList = [{'id': '123','name': 'one'},{'id': '-1123','name': 'two'},{'id': '566','name': 'three'}]
>>> nameToId = dict((e['name'], e['id']) for e in myList)
>>> nameToId[input("Enter a name: ")]
Enter a name: two
'-1123'
you can try it:你可以试试看:
myDict = {'values': [{'id': '123456','name': 'person1'}, {'id': '-1123678','name': 'person2'},{'id': '566','name': 'person3'}, {'id': '-1123678','name': 'person4'},{'id': '-1123678','name': 'person5'}]};
for i in range(len(myDict['values'])):
if(myDict['values'][i]['name'] == 'person3'):
print(myDict['values'][i]['id'])
or要么
myDict = {'values': [{'id': '123456','name': 'person1'}, {'id': '-1123678','name': 'person2'},{'id': '566','name': 'person3'}, {'id': '-1123678','name': 'person4'},{'id': '-1123678','name': 'person5'}]};
name = input("Enter name: \n")
for i in range(len(myDict['values'])):
if(myDict['values'][i]['name'] == name):
print(myDict['values'][i]['id'])
So you have your list所以你有你的清单
mylist = [{'id': '123','name': 'one'},{'id': '-1123','name': 'two'},{'id': '566','name': 'three'}]
Now, let's say you have defined a variable现在,假设您已经定义了一个变量
myname = "one"
All you have to do is search for the corresponding id
with that name:您所要做的就是搜索具有该名称的相应
id
:
[item["id"] for item in mylist if item["name"] == myname]
a = [{'id': '123','name': 'one'},{'id': '-1123','name': 'two'},{'id': '566','name': 'three'}]
name = input()
for i in a:
if i['name'] == name:
print(i['id'])
break
Maybe too late for the party but here is my take with error message if name is not found in the list:对于派对来说可能为时已晚,但如果在列表中找不到名称,这是我对错误消息的看法:
dict_arr = [{'id': '123','name': 'one'},{'id': '-1123','name': 'two'},{'id': '566','name': 'three'}]
def find_id(req_name):
for dic in dict_arr:
if dic['name'] == req_name:
print(dic['id'])
return
#if name not found in list:
print("Invalid name")
return
if __name__ == "__main__":
req_name = input("Enter name:\n")
find_id(req_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.