2016-05-23 143 views
-2

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

我能够非常容易地访问列表中的列表中的字典列表中的值。但是,由于列表中有许多元素,我必须使用for循环来完成它。基本上我的方法是检查1是否在列表中的字典中的索引(用户指定的数字)在该列表中。如果是,它会使用同一个字典中的前两个键值对更新另一个列表。

所以,像这样:

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循环:

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'。它已被修复。谢谢。

+0

可以共享整个错误堆栈? – glls

+0

@glls我100%肯定它的工作原理,因为当我尝试从returnExternalList打印列表中的这个方法,它的工作原理。我会更新它只是为了确保。 – user132520

+0

看起来像你想的类型错误指示遍历一个字典... – glls

回答

3

当你

for i in listRaw: 

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

所以你不必做listRaw[i]获取该物品。 i本身就是这个项目。更改代码相应

+0

改成了这样: '因为我在范围内(LEN(listRaw)): 如果listRaw [I] [“值”] [b] == 1: filteredList.update({listRaw [i] ['ID']:listRaw [i] ['Zip Code']})' 现在我只是得到一个空的字典作为输出。 – user132520

0

提示:调用listRaw [I]我必须为整数

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

,但我不为0的时候,你的循环做。

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

考虑增加你的代码中的一些版画,以帮助调试,开始

1

的问题时,这是非常方便的是,当你做

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} 
相关问题