【问题标题】:List Indices Must Be Integers or Slices, not Dict列表索引必须是整数或切片,而不是字典
【发布时间】:2016-09-19 18:55:18
【问题描述】:

我正在做一些涉及从每个元素都是字典的列表中提取数据的操作。每个字典包含两个键值对,它们是一个字符串,然后是一个 int(即 {'ID':0, 'Zip Code':9414}),然后是一个键值对,其中键是一个字符串,然后一个列表({'Value':[0,0,1,1,0,1]})

我可以很容易地在列表中的字典中访问该列表中的值。但是,由于列表中有一堆元素,我必须使用 for 循环来遍历它。基本上,我的方法所做的是检查 1 是否在列表中的 dict 中的索引(用户指定的数字)处。如果是,它会使用同一个字典中的前两个键值对更新另一个列表。

所以,像这样:

import returnExternalList #this method returns a list generated by an external method

def checkIndex(b):
    listFiltered = {}
    listRaw = returnExternalList.returnList #runs the method "returnList", which will return the list
    for i in listRaw:
        if listRaw[i]['Value'][b] == 1:
            filteredList.update({listRaw[i]['ID']: listRaw[i]['Zip Code']})
    print(filteredList)

checkIndex(1)

returnExternalList.returnList:
[{'ID':1 ,'Zip Code':1 ,'Value':[0,1,0,0,1]},{'ID':2 ,'Zip Code':2 ,'Value':[0,0,0,0,0]},{'ID':3,'Zip Code':3 ,'Value':[0,1,1,1,0]},{'ID':4 ,'Zip Code':4 ,'Value':[1,0,0,0,0]}]

expected output:
[{1:1 , 3:3}]

我可以非常简单地通过执行以下操作来访问 for 循环的列表 outside 内的字典内的列表中的值:

print(listRaw[0]['Value'][1]) would return 1, for example.

但是,当尝试使用 for 循环复制该行为以检查列表中的每一个时,我收到错误:

TypeError: list indices must be integers or slices, not dict

我该怎么办?

编辑:既然它被要求,returnExternalList:

def returnList:
    listExample = [{'ID':1 ,'Zip Code':1 ,'Value':[0,1,0,0,1]},{'ID':2 ,'Zip Code':2 ,'Value':[0,0,0,0,0]},{'ID':3,'Zip Code':3 ,'Value':[0,1,1,1,0]},{'ID':4 ,'Zip Code':4 ,'Value':[1,0,0,0,0]}]
     return listExample  

编辑:我使用了下面提供的两种解决方案,虽然它确实消除了错误(谢谢!)但输出只是一个空白字典。

代码:

for i in listRaw:
    if i['Value'][b] == 1:
       filteredList.update({i['ID']: i['Zip Code']})

or

for i in range(len(listRaw):
    if listRaw[i]['Value'][b] == 1:
        filteredList.update({listRaw[i]['ID']: listRaw[i]['Zip Code']}) 

编辑:

现在可以了,列表为空的原因是因为我将 1 与“1”进行比较。它已被修复。谢谢。

【问题讨论】:

  • 可以共享整个错误堆栈吗?
  • @glls 我 100% 确定它有效,因为当我尝试在此方法中从 returnExternalList 打印列表时,它有效。为了确定,我会更新它。
  • 看起来你正试图迭代一个字典,因为 TypeError 表明......

标签: python list dictionary indices


【解决方案1】:

当你这样做时

for i in listRaw:

i 不是索引,它是列表中的实际项目(在你的情况下它是一个字典)

所以您不必通过listRaw[i] 来获取该项目。 i 本身就是项目。相应地更改您的代码

【讨论】:

  • 将其更改为:for i in range(len(listRaw)): if listRaw[i]['Value'][b] == 1: filteredList.update({listRaw[i]['ID']: listRaw[i]['Zip Code']}) 现在我只是得到一个空字典作为输出。
【解决方案2】:

问题是当你这样做时

for i in listRaw:

这不会为您提供i 中的ist 索引,它会为您提供列表中的实际字典。循环的每次迭代都会为您提供列表中的下一个字典。您可以通过以下方式修复它:

def checkIndex(b):
    filteredList = {}
    listRaw = returnExternalList.returnList #runs the method "returnList", which will return the list
    for d in listRaw:
        if d['Value'][b] == 1:
            filteredList.update({d['ID']: d['Zip Code']})
    print(filteredList)

但整件事可以更简洁地写成字典理解:

def checkIndex(b):
    print({d['ID']: d['Zip Code'] for d in returnExternalList.returnList if d['Value'][b] == 1})

我倾向于让你的函数接受列表作为参数而不是访问全局变量,并返回结果:

def check_index(index, return_list):
    return {d['ID']: d['Zip Code'] for d in return_list if d['Value'][index] == 1}

>>> print(check_index(1, returnExternalList.returnList))
{1: 1, 3: 3}

【讨论】:

    【解决方案3】:

    提示:调用 listRaw[i] i 必须是整数

    listRaw[0]['Value'][1] == 1
    

    但执行 for 循环时 i 不为 0。

    在你的尝试中 for i in range(len(listRaw)), 你的听写。是空的,因为 (len(listRaw)) 的长度是...1,所以您的迭代并没有真正迭代所有字典元素。

    考虑在代码中添加一些打印以帮助调试,这在启动时非常方便

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 2020-06-17
      • 2020-07-11
      • 1970-01-01
      • 2022-10-04
      • 2017-06-08
      • 2016-05-25
      • 2016-09-16
      • 2017-10-01
      相关资源
      最近更新 更多