将列表列表转换为字典

时间:2019-07-18 10:43:34

标签: python

我有一个包含六个列表的列表

lst = [
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']
]

我正在尝试将其转换为

{
    "2.2.5": ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.5',
              '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5',
              '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.5'],
    "2.2.6": ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.6',
              '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.6',
              '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.6']
}

到目前为止,这是我所做的:

to_dict = {key[0]: [value[1] + '/' + value[0] for value in lst] for key in lst}

这将返回

{'2.2.5': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.6'],
 '2.2.6': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.6']}

内部循环将从索引1的索引lst开始添加所有内容。我应该如何添加适合其键的值?

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作。

lst = [
['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'],
['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']
]
from collections import defaultdict
c=defaultdict(list)
[c[i].append(j) for i,j in lst]
print c

输出: defaultdict(<type 'list'>, {'2.2.5': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'], '2.2.6': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']})

答案 1 :(得分:1)

使用defaultdictdict.setdefault()方法:

lst = [
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All'],
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App'],
    ['2.2.5', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'],
    ['2.2.6', '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']
]

out = {}
for k, v in lst:
    out.setdefault(k, []).append(v + '/' + k)

from pprint import pprint
pprint(out, width=30)

打印:

{'2.2.5': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.5'],
 '2.2.6': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.6',
           '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.2.6']}

答案 2 :(得分:0)

您可以遍历列表元素作为键值(python进行解压缩),然后使用 dict.setdefault 将值列表设置为空并追加到列表中:

In [77]: out = {}                                                                                                                                                                                           

In [78]: for k, v in lst: 
    ...:     out.setdefault(k, []).append(v) 
    ...:                                                                                                                                                                                                    

In [79]: out                                                                                                                                                                                                
Out[79]: 
{'2.2.5': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All',
  '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App',
  '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'],
 '2.2.6': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All',
  '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App',
  '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']}

collections.defaultdict ,工厂为list

In [80]: out = collections.defaultdict(list)                                                                                                                                                                

In [81]: for k, v in lst: 
    ...:     out[k].append(v) 
    ...:      
    ...:                                                                                                                                                                                                    

In [82]: out                                                                                                                                                                                                
Out[82]: 
defaultdict(list,
            {'2.2.5': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All',
              '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App',
              '/usr/local/share/dotnet/shared/Microsoft.NETCore.App'],
             '2.2.6': ['/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All',
              '/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App',
              '/usr/local/share/dotnet/shared/Microsoft.NETCore.App']})

要将键字符串附加到值,请使用f字符串或str.format(在不支持f字符串的版本上):

对于dict.setdefault

out.setdefault(k, []).append(f'{v}/{k}')
out.setdefault(k, []).append('{v}/{k}'.format(v=v, k=k))

对于collections.defaultdict

out[k].append(f'{v}/{k}')
out[k].append('{v}/{k}'.format(v=v, k=k))