首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -在数组中搜索特定的值

Python -在数组中搜索特定的值
EN

Stack Overflow用户
提问于 2019-10-16 17:37:40
回答 1查看 40关注 0票数 0

为了获取一些信息,我使用Pythonrequests库调用API。到目前一切尚好。代码运行良好,响应成功:

请求:

代码语言:javascript
复制
def countries():
    # Application header
    header = {
        'Authorization': '123456789',
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # Make the request and return the country code
    request_url = requests.get(
        'https://endpoint/api/Countries', headers=header)

    countries_dictionary = request_url.json()

countries()

我将响应存储到变量中,并将其编码到JSON中。当我打印变量的值时,结果如下:

代码:

代码语言:javascript
复制
print(countries_dictionary)

响应:

代码语言:javascript
复制
[
{'CountryId': 1, 'CountryName': 'Belgium', 'Abbreviation': 'B'}, 
...............................................................
...............................................................
{'CountryId': 26, 'CountryName': 'Great Britain', 'Abbreviation': 'GB'}
]

根据以括号开头的结果,响应是一个列表。根据PythonJSON之间的等价物,列表被翻译成数组。但是,如果我想提取第一个键的值,就会得到一个错误:

代码语言:javascript
复制
for country in countries_dictionary['CountryId'][0]:
    if country['CountryId']=='1':
        print('Test')

错误:

文件“c:/Python3.7/api.py”,第49行,国家中的国家_字典‘country’:TypeError:字符串索引必须是整数

我知道索引是用整数数据类型表示的,而且由于我得到的响应没有任何父节点,所以我有点迷路了,不知道下一步如何进行。到目前为止,我在Stackoverflow或任何其他网站上发现的任何东西都没有起作用。有小费吗?

编辑:

代码语言:javascript
复制
[
{'CountryId': 1, 'CountryName': 'Romania', 'Abbreviation': 'RO'}, 
{'CountryId': 2, 'CountryName': 'Bulgaria', 'Abbreviation': 'BG'}, 
{'CountryId': 3, 'CountryName': 'Slovenia', 'Abbreviation': 'SI'}, 
{'CountryId': 4, 'CountryName': 'Olanda', 'Abbreviation': 'NL'}, 
{'CountryId': 5, 'CountryName': 'Polonia', 'Abbreviation': 'PL'}, 
{'CountryId': 6, 'CountryName': 'Belgia', 'Abbreviation': 'BE'}, 
{'CountryId': 7, 'CountryName': 'Germania', 'Abbreviation': 'DE'}, 
{'CountryId': 8, 'CountryName': 'Spania', 'Abbreviation': 'ES'}, 
{'CountryId': 9, 'CountryName': 'Portugalia', 'Abbreviation': 'PT'}, 
{'CountryId': 10, 'CountryName': 'Irlanda', 'Abbreviation': 'IE'}, 
{'CountryId': 11, 'CountryName': 'Austria', 'Abbreviation': 'AT'}, 
{'CountryId': 12, 'CountryName': 'Croatia', 'Abbreviation': 'HR'}, 
{'CountryId': 13, 'CountryName': 'Ungaria', 'Abbreviation': 'HU'}, 
{'CountryId': 14, 'CountryName': 'Suedia', 'Abbreviation': 'SE'}, 
{'CountryId': 15, 'CountryName': 'Slovacia', 'Abbreviation': 'SK'},
{'CountryId': 16, 'CountryName': 'Cehia', 'Abbreviation': 'CZ'}, 
{'CountryId': 17, 'CountryName': 'Danemarca', 'Abbreviation': 'DK'}, 
{'CountryId': 18, 'CountryName': 'Estonia', 'Abbreviation': 'EE'}, 
{'CountryId': 19, 'CountryName': 'Finlanda', 'Abbreviation': 'FI'}, 
{'CountryId': 20, 'CountryName': 'Franta', 'Abbreviation': 'FR'}, 
{'CountryId': 21, 'CountryName': 'Letonia', 'Abbreviation': 'LV'}, 
{'CountryId': 22, 'CountryName': 'Lituania', 'Abbreviation': 'LT'}, 
{'CountryId': 23, 'CountryName': 'Luxemburg', 'Abbreviation': 'LU'}, 
{'CountryId': 24, 'CountryName': 'Norvegia', 'Abbreviation': 'NO'}, 
{'CountryId': 25, 'CountryName': 'Marea Britanie', 'Abbreviation': 'GB'}, 
{'CountryId': 26, 'CountryName': 'Ucraina', 'Abbreviation': 'UA'}
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 17:45:53

根据您的字典示例列表,您的代码中出现了一些错误。

  • 您不需要索引,因为您正在遍历列表

  • 您不能在for循环定义

中指定“CountryId”

  • 'CountryId‘的字典值是整数,而不是字符串

但这应该是的诀窍

代码语言:javascript
复制
countries_dictionary = [{'CountryId': 1, 'CountryName': 'Belgium', 'Abbreviation': 'B'}, 
                        {'CountryId': 26, 'CountryName': 'Great Britain', 'Abbreviation': 'GB'}]
for country in countries_dictionary:
    if country['CountryId']==1:
        print('Test')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58418867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档