代码之家  ›  专栏  ›  技术社区  ›  Bill Armstrong

python-遍历列表字典

  •  2
  • Bill Armstrong  · 技术社区  · 8 年前

    我有一本字典,是用一系列 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 s初始状态。那么 '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...' 对非唯一的 '项目描述' 新的属性值被附加到现有的项中。

    我试过什么…好吧,我尝试在找到唯一项时迭代“record”对象,并使用如下方式附加一个假值:

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

    但(i)添加 错误 对于随后的唯一项和(i i)我需要列表保持有序-所以简单地遍历末尾的所有内容并强制为列表指定数量的元素对我没有任何好处。

    感谢您的帮助。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Ajax1234    8 年前

    您可以使用字典理解:

    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]}}
    
        2
  •  3
  •   rafaelc    8 年前

    听写理解是一个很好的解决方案,而且是纯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]
        }
    }