展平python中每个键的值

时间:2017-06-09 10:31:47

标签: python python-2.7 dictionary flatten

我有这样的字典:

migration_dict = {'30005': ['key42750','key43119', 'key44103', ['key333'],
['key444'], ['keyxx']], '30003': ['key43220', 'key42244','key42230',
['keyzz'], ['kehh']]}

如何压缩每个键的值以便得到类似的东西:

migration_dict = {'30005': ['key42750','key43119', 'key44103', 'key333',
'key444', 'keyxx'], '30003': ['key43220', 'key42244','key42230',
'keyzz', 'kehh']}

5 个答案:

答案 0 :(得分:9)

您可以编写一个递归函数来展平值列表,并在词典理解中使用它来构建新词典:

def flatten(lst):
   for x in lst:
      if isinstance(x, list):
         for y in flatten(x): # yield from flatten(...) in Python 3
            yield y           #
      else:
         yield x

migration_dict = {k: list(flatten(v)) for k, v in dct.items()}
print(migration_dict)
# {'30005': ['key42750', 'key43119', 'key44103', 'key333', 'key444', 'keyxx'], '30003': ['key43220', 'key42244', 'key42230', 'keyzz', 'kehh']}

它处理dict值列表中的任何嵌套深度。

答案 1 :(得分:2)

for key in migration_dict:
    for i in migration_dict[key]:
        if type(i) == list:
            migration_dict[key].remove(i)
            for element in i:
                migration_dict[key].append(element)

现在这个循环应该这样做。但请注意,仅当内部列表中没有更多列表时,此方法才有效。如果确实如此,那么你可能需要制作一个递归函数,为你展平它。

答案 2 :(得分:2)

如果您不介意使用第三方扩展程序,可以使用iteration_utilities.deepflatten 1 和字典理解:

>>> from iteration_utilities import deepflatten
>>> {key: list(deepflatten(value, ignore=str)) for key, value in migration_dict.items()}
{'30003': ['key43220', 'key42244', 'key42230', 'keyzz', 'kehh'],
 '30005': ['key42750', 'key43119', 'key44103', 'key333', 'key444', 'keyxx']}

这会使您的值中的所有可迭代项变平(字符串除外)。

1 免责声明:我是该图书馆的作者

答案 3 :(得分:0)

Heres a oneliner:

考虑这个dict,结构相同:

m =  {'a': ['k1', 'k2', 'k3', ['k4'], ['k5'], ['k6']],
     'b': ['k1', 'k2', 'k3', ['k4'], ['k5'], ['k6']]}

import itertools

d = {k:list(itertools.chain.from_iterable(itertools.repeat(x,1) if isinstance(x, str) else x for x in v)) for k,v in m.iteritems()}

{'a': ['k1', 'k2', 'k3', 'k4', 'k5', 'k6'],
 'b': ['k1', 'k2', 'k3', 'k4', 'k5', 'k6']}

您还可以使用第三方库more-iterools

t = {k: list(more_itertools.collapse(v, base_type=str)) for k,v in m.iteritems()}

 {'a': ['k1', 'k2', 'k3', 'k4', 'k5', 'k6'],
 'b': ['k1', 'k2', 'k3', 'k4', 'k5', 'k6']}

答案 4 :(得分:0)

如果在字典中使用平面值很重要,也许你可以创建一个函数来删除所有(多维)列表,然后再将它们添加到字典中?

def flatten_values(value):
      if type(value) == list:
           for i in value:
                return i

希望这有帮助,无法将其变成评论,因为我没有足够的代表;)