Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表列表转换为字典_Python_List_Dictionary - Fatal编程技术网

Python 将列表列表转换为字典

Python 将列表列表转换为字典,python,list,dictionary,Python,List,Dictionary,我有一份清单,清单如下: list1 = [ [0, 4, 2, 5, 0, 0], [4, 0, 0, 0, 6, 4], [2, 0, 0, 7, 0, 0], [5, 0, 7, 0, 5, 3], [0, 6, 0, 5, 0, 1], [0, 4, 0, 3, 1, 0], ] 我想通过以下方式将其

我有一份清单,清单如下:

list1 = [  [0,  4,  2,  5,  0,  0],
           [4,  0,  0,  0,  6,  4],
           [2,  0,  0,  7,  0,  0],
           [5,  0,  7,  0,  5,  3],
           [0,  6,  0,  5,  0,  1],
           [0,  4,  0,  3,  1,  0],
        ]
我想通过以下方式将其转换为字典:

G = {'a' : { 'b':4, 'c':2, 'd':5},
     'b': {'a':4,'e':  6,'f':4},
     'c':{ 'a':2,'d':7},
     'd':{'a':5, 'c':7, 'e':5,'f':3},
     'e':{'b':6, 'd':5,'f':1},
     'f' :{'b':4, 'd':3,'e':1}
 }
我有下面的代码来做这件事,但它只是给我一个错误,不可破坏的类型列表:

 list1 = [  [0,  4,  2,  5,  0,  0],
  [4,  0,  0,  0,  6,  4],
  [2,  0,  0,  7,  0,  0],
  [5,  0,  7,  0,  5,  3],
  [0,  6,  0,  5,  0,  1],
  [0,  4,  0,  3,  1,  0],
]

g=['a','b','c','d', 'e','f']
dl = zip(list1, g)
dict(dl)
print dl
错误是:

   File "/Users/zakirtatari/Documents/da.py", line 19, in <module>
      dict(dl)
   TypeError: unhashable type: 'list'

下面的解决方案有点难看,可以通过使用enumerate和zip进行美化,但它似乎可以实现您希望它实现的功能:

alphabet = "abcdefghijklmnopqrstuvwxyz"
list1 = [[0,4,2,5,0], [4, 0, 0, 0, 6]]

g = {}
for idx in range(len(list1)):
    g[alphabet[idx]] = {}
    for idx_2 in range(len(list1[idx])):
        if list1[idx][idx_2] != 0:
            g[alphabet[idx]][alphabet[idx_2]] = list1[idx][idx_2]

下面的解决方案有点难看,可以通过使用enumerate和zip进行美化,但它似乎可以实现您希望它实现的功能:

alphabet = "abcdefghijklmnopqrstuvwxyz"
list1 = [[0,4,2,5,0], [4, 0, 0, 0, 6]]

g = {}
for idx in range(len(list1)):
    g[alphabet[idx]] = {}
    for idx_2 in range(len(list1[idx])):
        if list1[idx][idx_2] != 0:
            g[alphabet[idx]][alphabet[idx_2]] = list1[idx][idx_2]

如果您不关心具有零值的键,这应该可以做到:

from string import ascii_lowercase as letters
dict(zip(letters, [dict(zip(letters, L)) for L in list1]))
如果要筛选出此类密钥,可以执行以下操作:

dict(zip(letters,
         [dict((k, v) for k, v in zip(letters, L) if v) for L in list1]))

如果您不关心具有零值的键,这应该可以做到:

from string import ascii_lowercase as letters
dict(zip(letters, [dict(zip(letters, L)) for L in list1]))
如果要筛选出此类密钥,可以执行以下操作:

dict(zip(letters,
         [dict((k, v) for k, v in zip(letters, L) if v) for L in list1]))

我不确定我是否会在实践中用一行字来表达这一点,但这里有一行,使用字典理解:

{key: {key2: value for key2, value in zip(g, row) if value != 0}
 for key, row in zip(g, list1)}

我不确定我是否会在实践中用一行字来表达这一点,但这里有一行,使用字典理解:

{key: {key2: value for key2, value in zip(g, row) if value != 0}
 for key, row in zip(g, list1)}

首先,我将编写两个函数,一个将列表转换为字典,从ascii_小写字母中获取键,另一个遍历字典并过滤出值为零的键值对

from string import ascii_lowercase

def list_to_dict(in_list):
    return dict([(k, v) for k, v in zip(ascii_lowercase, in_list)])

def filter_dict(in_dict):
    return dict([(k, v) for k, v in in_dict.items() if v != 0])
然后,所有逻辑都可以放入一个列表迭代器语句中:

list1 = list_to_dict([filter_dict(list_to_dict(x)) for x in list1 ])
首先,检查每个“行”并将其转换为字典,然后根据等于零的值筛选字典

对字典列表运行相同的函数,并将它们转换为键值对


如果要保留元素的顺序,您可能需要使用OrderedDict。

首先,我将编写两个函数,一个将列表转换为字典,从ascii_小写中获取键,另一个通过字典筛选出值为零的键值对

from string import ascii_lowercase

def list_to_dict(in_list):
    return dict([(k, v) for k, v in zip(ascii_lowercase, in_list)])

def filter_dict(in_dict):
    return dict([(k, v) for k, v in in_dict.items() if v != 0])
然后,所有逻辑都可以放入一个列表迭代器语句中:

list1 = list_to_dict([filter_dict(list_to_dict(x)) for x in list1 ])
首先,检查每个“行”并将其转换为字典,然后根据等于零的值筛选字典

对字典列表运行相同的函数,并将它们转换为键值对


如果要保留元素的顺序,可能需要使用OrderedDict。

显示错误。另外,解释一下你认为你的代码应该如何工作。这是你唯一关心的例子吗?是否存在一种更常见的情况,即您有超过26行/列,并且可能没有字母?显示错误。另外,解释一下你认为你的代码应该如何工作。这是你唯一关心的例子吗?是否存在一种更普遍的情况,即您有超过26行/列,并且可能没有字母?