使用for循环变量选择元组值(Python)

时间:2019-05-23 14:11:51

标签: python

我正在python中创建一个for循环,以遍历元组中的值。我的代码是:

for x in SageResult:
    SOPOrderReturnID=SageResult[x]

这会产生错误:

InsertError: list indices must be integers or slices, not tuple

x替换为0可以正常工作,但是使用循环值不起作用。我该如何解决?

4 个答案:

答案 0 :(得分:3)

indexNamePrefix遍历元组,并将元组的每个元素写入# Declare the indexed type INSERT_UPDATE SolrIndexedType;identifier[unique=true];type(code); ;myMediaType;Media; ;myMediaVideoType;Media; # Declare the FacetSearchConfig INSERT_UPDATE SolrFacetSearchConfig;name[unique=true];description;indexNamePrefix; ;myMediaTypeFSC;facetSearchConfigDescription1;myMediaType; ;myMediaVideoTypeFSC;facetSearchConfigDescription2;myMediaVideoType; 。因此,您应该在循环中编写以下代码:

for x in SageResult:

但是,如果要使用元组的索引,则应使用enumerate函数:

x

range

SOPOrderReturnID=x

答案 1 :(得分:1)

for x in SageResult:
     SOPOrderReturnID=x

或者:

for x in range(len(SageResult)):
     SOPOrderReturnID=SageResult[x]

答案 2 :(得分:0)

您的x不是整数。我假设您可以在以下代码中重写循环:

for x in range(0, len(SageResult)):

答案 3 :(得分:-1)

在这种情况下,请循环访问索引,而不是数据。

site_num