Python - 遍历列表字典

时间:2018-06-16 21:14:36

标签: python python-3.x

我有一个使用一系列for循环生成的字典。结果看起来像这样:

{
'item1': {
    'attribute1': [3],
    'attribute2': [2],
    'attribute3': [False],
    },
'item2': {
    'attribute1': [2, 5, 2],
    'attribute2': [3, 2, 8],
    'attribute3': [False, 7, False],
    },
'item3': {
    'attribute1': [8],
    'attribute2': [4],
    'attribute3': [False],
    },
}

False中的'attribute3'节目是将空值传递到item初始状态的结果。然后通过两次迭代更新'item2'

我想要做的是让每个属性的列表具有相同的长度,以便所需的输出为:

{
'item1': {
    'attribute1': [3, False, False],
    'attribute2': [2, False, False],
    'attribute3': [False, False, False],
    },
'item2': {
    'attribute1': [2, 5, 2],
    'attribute2': [3, 2, 8],
    'attribute3': [False, 7, False],
    },
'item3': {
    'attribute1': [8, False, False],
    'attribute2': [4, False, False],
    'attribute3': [False, False, False],
    },
}

供参考 - 初始条目的代码检查以确保item_desc是唯一的,如果是,则生成新条目 - 它看起来像这样:

record.update({item_desc: {
    'attribute1':[],
    'attribute2':[],
    'attribute3':[],
    }})
for key, value in [
    ('attribute1', value1),
    ('attribute2', value2),
    ('attribute3', value3)]:
    record[item_desc][key].append(value)

如果'item_desc'不唯一,则会针对非唯一'for key, value in...'再次运行'item_desc',并将新属性值附加到现有项目。

我尝试了什么......好吧,当我找到一个独特的项目时,我尝试迭代“记录”对象并使用以下内容附加一个False值:

for item in record:
    for key in ['attribute1', 'attribute2', 'attribute3']:
    record[item][key].append(False)

但是(i)它没有解决为后续独特项目添加False和(ii)我需要列表保持秩序 - 所以简单地迭代通过我没有任何好处最后的一切,强制列表的给定数量的元素。

感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

dict理解是一个很好的解决方案和纯粹的python。

只是出于选项问题,您还可以使用pandas等库。

df = pd.DataFrame(d)
max_ = df.max().str.len().max() # max length (in this case, 3)
df.transform(lambda x: [z + [False]*(max_ - len(z)) for z in x]).to_dict()

输出

{'item1': 
    {'attribute1': [3, False, False],
     'attribute2': [2, False, False],
     'attribute3': [False, False, False]
    },
 'item2': 
    {'attribute1': [2, 5, 2],
     'attribute2': [3, 2, 8],
     'attribute3': [False, 7, False]
     },
 'item3': 
    {'attribute1': [8, False, False],
     'attribute2': [4, False, False],
     'attribute3': [False, False, False]
    }
}

答案 1 :(得分:1)

您可以使用词典理解:

d = {
    'item1': {
        'attribute1': [3], 
        'attribute2': [2], 
        'attribute3': [False]}, 
    'item2': {
        'attribute1': [2, 5, 2], 
        'attribute2': [3, 2, 8], 
        'attribute3': [False, 7, False]}, 
    'item3': {
        'attribute1': [8], 
        'attribute2': [4], 
        'attribute3': [False]}}
adjust = max(len(max([c['attribute1'], c['attribute2'], c['attribute3']])) for c in d.values())
new_d = {a:{c:j+([False]*(adjust-len(j))) for c, j in b.items()} for a, b in d.items()}

输出:

{
'item1': {
    'attribute1': [3, False, False], 
    'attribute2': [2, False, False], 
    'attribute3': [False, False, False]}, 
'item2': {
    'attribute1': [2, 5, 2], 
    'attribute2': [3, 2, 8], 
    'attribute3': [False, 7, False]}, 
'item3': {
    'attribute1': [8, False, False], 
    'attribute2': [4, False, False], 
    'attribute3': [False, False, False]}}