如何制作具有多个列表的词典字典

时间:2017-06-14 06:27:23

标签: python dictionary

有一个索引列表将成为父词典的键:

index = [1,2,3]

然后多个列表将成为子序列:

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]

顺序元素是数据,导致:

{1:{'triangles':4, 'circles':7, 'squares': 10},
 2: {'triangles': 5, 'circles': 8, 'squares': 11},
 3: {'triangles': 6, 'circles': 9, 'squares': 12}}

我该怎么做?

你认为熊猫更容易吗?

7 个答案:

答案 0 :(得分:4)

您可以zip列表,创建子区域,然后用索引压缩子区域。指数没有限制;它们可以是非顺序/非数字的:

dct =  dict(zip(index, ({'triangles': i, 'circles': j, 'squares': k} 
                          for i,j,k in zip(triangles, circles, squares))))
print(dct)
{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

另一方面,如果您只需要连续计数,则索引列表可以替换为enumerate

dct =  dict(enumerate(({'triangles': i, 'circles': j, 'squares': k} 
                          for i,j,k in zip(triangles, circles, squares)), 1))

答案 1 :(得分:1)

Dict comprehnesions to rescue! 请注意,顺便说一句,存储在index中的索引似乎是基于一个,尽管python列表是从零开始的:

result =  {i : {'triangles' : triangles[i-1], 'circles' : circles[i-1], 'squares' : squares[i-1]} for i in index}

答案 2 :(得分:1)

最简单的方法是使用dict comprehension

>>> d = {i:{'triangles':triangles[i-1],'circles':circles[i-1],'squares':squares[i-1]} for i in index}

{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

答案 3 :(得分:0)

实际上这非常简单,可以通过简单的for循环来实现:

index = [1,2,3]

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]

dictionary = {}

for i in range(0, len(index)):
    dictionary[index[i]] = {'triangles':triangles[i], 'circles':circles[i], 'squares':squares[i]}

print(dictionary)

输出:

  

{1:{'triangles':4,'circles':7,'squares':10},2:{'triangles':5,'circles':8,'squares':11},3: {'triangles':6,'circles':9,'squares':12}}

答案 4 :(得分:0)

你可以这样做,

results = {}
for index, item in enumerate(zip(triangles,circles,squares)):
    results.update({index+1:{'triangles':item[0], 'circles':item[1], 'squares':item[2]}})

Out[6]: 
{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

答案 5 :(得分:0)

如果你有很多变数并且你不想硬编码你的字典理解,这是一种方法。

注意:您需要声明所有变量。

您还需要声明变量名列表。

list_of_var_names = ['triangles', 'circles', 'squares']

dict(zip(index, [dict(zip(list_of_var_names, i)) 
             for i in (globals().get(i) for i in list_of_var_names)]))

并逐步分开:

In [1]: index = [1,2,3]
   ...:
   ...: triangles = [4,5,6]
   ...: circles = [7,8,9]
   ...: squares = [10,11,12]
   ...:

In [2]: list_of_var_names = ['triangles', 'circles', 'squares']

In [3]: [globals().get(i) for i in list_of_var_names]  # getting list of variable values in list_of_var_names order
Out[3]: [[4, 5, 6], [7, 8, 9], [10, 11, 12]]

In [4]: [dict(zip(list_of_var_names, i)) for i in (globals().get(i) for i in lis
   ...: t_of_var_names)]
Out[4]:
[{'circles': 5, 'squares': 6, 'triangles': 4},
 {'circles': 8, 'squares': 9, 'triangles': 7},
 {'circles': 11, 'squares': 12, 'triangles': 10}]

In [5]: dict(zip(index, [dict(zip(list_of_var_names, i))
   ...:                  for i in (globals().get(i) for i in list_of_var_names)]
   ...: ))
   ...:
Out[5]:
{1: {'circles': 5, 'squares': 6, 'triangles': 4},
 2: {'circles': 8, 'squares': 9, 'triangles': 7},
 3: {'circles': 11, 'squares': 12, 'triangles': 10}}

我想再提一次,如果你得到大量的变数并且你不想明确声明dict理解,这个解决方案是好的。在其他情况下,使用此处提供的其他解决方案会更合适,更具可读性。

答案 6 :(得分:0)

另一种使用双zip()dict comprehension的种类:

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]
index = [1,2,3]

b = {k:{'triangles': x, 'circles': y, 'squares': z} for k, (x,y,z) in zip(
                                  index, zip(triangles, circles, squares))}
print(b)

输出:

{1: {'circles': 7, 'squares': 10, 'triangles': 4}, 
2: {'circles': 8, 'squares': 11, 'triangles': 5}, 
3: {'circles': 9, 'squares': 12, 'triangles': 6}}