Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 遍历字符串列表,并为列表中的每个术语添加json返回的元素_Python_Json - Fatal编程技术网

Python 遍历字符串列表,并为列表中的每个术语添加json返回的元素

Python 遍历字符串列表,并为列表中的每个术语添加json返回的元素,python,json,Python,Json,警告:这非常复杂。(我是个白痴,可能应该休息一下),这是一个令人困惑的标题,但我会尽力解释我遇到了什么 我有以下代码: test_terms = ['spartan', 'forerunner', 'didact'] test_list = [] for term in test_terms: search_term = term search_service = PyMsCognitiveWebSearch('API_Key', search_term,

警告:这非常复杂。(我是个白痴,可能应该休息一下),这是一个令人困惑的标题,但我会尽力解释我遇到了什么

我有以下代码:

test_terms = ['spartan', 'forerunner', 'didact']

test_list = []

for term in test_terms:

    search_term = term
    search_service = PyMsCognitiveWebSearch('API_Key', search_term,
                                        custom_params={'title': 'name', 'domain': 'url', 'mkt': 'en-usa', 'description': 'url'})
    result = search_service.search(limit=3, format='json')
    test_list.append(result[term].json)

print(test_list)
当它在循环之外时,它就工作了,我只运行以下命令:

search_term = 'spartan'
search_service = PyMsCognitiveWebSearch('API_Key', search_term,
                                        custom_params={'title': 'name', 'domain': 'url', 'mkt': 'en-usa',
                                                       'description': 'url'})
result = search_service.search(limit=3, format='json')
test_list.append(result[0].json)
print(test_list)
这给了我:

[{'id': 'https://api.cognitive.microsoft.com/api/v7/#WebPages.0', 'name': 'Obstacle Course Races | Spartan Race', 'url': 'https://www.spartan.com/', 'isFamilyFriendly': True, 'displayUrl': 'https://www.spartan.com', 'snippet': 'Spartan Race is the global leader in obstacle course races, with the right challenge for anyone - from first-time racers to elite athletes.', 'deepLinks': ... etc, for three more results}]
我的第一反应不是简单地对范围内的术语(len(test_terms))执行
,然而这返回了一个结果,但不是列表中的项目,而是关于数字1到3的信息

接下来我尝试了
项、术语枚举(test\u terms):

但我有一个错误:

TypeError: list indices must be integers or slices, not str
这是有意义的,但是我不确定如何继续,因为索引是一个“字符串”,我尝试用两种不同的方式枚举它,删除
result[term].json
并将其更改为
result.json
也会抛出一个对象错误

任何帮助都会很好!如果有帮助的话,还可以提供规格

更新:

result
是一个web对象,本质上是这样的(通过文档):


但它是一个巨大的嵌套列表,我在白板上写下它,它返回3个列表,由9个元素组成,混合类型,一个列表和一个字典。字面上说是我的噩梦。

在for循环中使用“term”的方式使“term”成为一个字符串(一个['spartan'、'forerunner'、'didact'])。您正在寻找在“result[term]”中使用的整数,因此,请坚持使用enumerate,但请记住使用enumerate元组的第一部分是“枚举”,第二部分是项

for i, term in enumerate(test_terms):
    ...
    test_list.append(result[i].json)

你试过测试术语中的术语吗??是的,那是我第一次尝试。结果[term].json应该是什么?为什么不像代码的工作版本那样使用
result[0].json
?@Aran Fey,它只返回查询的第0个元素,理想情况下应该是0、1、2,每个元素都是类型为
的独立对象,可以跳过其他两个元素。我是个白痴,可能应该离开计算机一段时间。非常感谢。
for i, term in enumerate(test_terms):
    ...
    test_list.append(result[i].json)