简体   繁体   English

Python字典-将数组附加到特定键的字典中。

[英]Python dictionaries - appending arrays to a dictionary for a specific key.

This is about appending arrays to a dictionary with a specific key. 这是关于使用特定键将数组追加到字典。

Aim is to assign a number of arrays for a specific key in a dictionary: 目的是为字典中的特定键分配多个数组:

{1: [1,2,3,4], [6,3,2,3], 2: None, 3: None}

There's a dictionary 有字典

my_dict = dict.fromkeys([i for i in range(1,4)]
my_dict
{1: None, 2: None, 3: None}

I have a number of arrays: 我有很多数组:

array1 = [1,2,3,4]  
array2 = [6,3,2,3]
array3 = [5,7,11,15]

I wish to associate array1 with key = 1 我希望将array1与key = 1关联

my_dict[1] = array1
#the content of the my_dict is:
my_dict
{1: [1, 2, 3, 4], 2: None, 3: None}

Then I wish to add array2 associated with key 1. BUT if I do this 然后,我希望添加与键1关联的array2。但是,如果这样做,

my_dict[1].append(array2); 
#I get
my_dict
{1: [1, 2, 3, 4, [6,3,2,3]], 2: None, 3: None}

In other words I would like to know how to append for a specific key such that I get the following 换句话说,我想知道如何为特定的密钥附加内容,以便获得以下内容

{1: [1, 2, 3, 4], [6, 3, 2, 3], 2: None, 3: None}

NOT

{1: [1, 2, 3, 4, [6,3,2,3]], 2: None, 3: None}

In sum , I would like to append a number of arrays to a dictionary for a specific key such that I get: 总而言之 ,我想为特定键将多个数组添加到字典中,这样我得到:

{1: [1,2,3,4], [6,3,2,3], 2: None, 3: None}

Thank you, Anthony of Sydney. 谢谢悉尼的安东尼。

Your syntax is incorrect. 您的语法不正确。 What you likely need is a defaultdict of lists. 您可能需要的是列表的defaultdict值。

Then just use list.append each time you wish to add to a list of lists. 然后,每次要添加到列表列表时,只需使用list.append即可。

from collections import defaultdict

array1 = [1,2,3,4]  
array2 = [6,3,2,3]
array3 = [5,7,11,15]

my_dict = defaultdict(list)
my_dict[1].append(array1)
my_dict[1].append(array2)

defaultdict(list, {1: [[1, 2, 3, 4],
                       [6, 3, 2, 3]]})

If, on the other hand, you want a single list and just wish to add elements, use list.extend : 另一方面,如果您只需要一个列表,而只希望添加元素,请使用list.extend

my_dict = defaultdict(list)
my_dict[1].extend(array1)
my_dict[1].extend(array2)

# defaultdict(list, {1: [1, 2, 3, 4, 6, 3, 2, 3]})

I do not think you can achieve it. 我认为您无法实现。

{1: [1,2,3,4], [6,3,2,3], 2: None, 3: None}

but you can get: 但您可以获得:

{1: [[1,2,3,4], [6,3,2,3]], 2: [], 3: []}

my_dict = {i:[]for i in range(1,4)}
my_dict[1].append(array1)
my_dict[1].append(array2)

I'm guessing that what you want is {1: [[1, 2, 3, 4], [6, 3, 2, 3]], 2: None, 3: None} . 我猜想您想要的是{1: [[1, 2, 3, 4], [6, 3, 2, 3]], 2: None, 3: None} Then you just have to do: my_dict[1] = [array1, array2] . 然后,您只需要做: my_dict[1] = [array1, array2] Otherwise if what you want is {1: [1, 2, 3, 4, 6, 3, 2, 3], 2: None, 3: None} you have to do: my_dict[1] += array2 (once you have already done my_dict[1] = array1 ). 否则,如果您想要的是{1: [1, 2, 3, 4, 6, 3, 2, 3], 2: None, 3: None} ,则必须执行: my_dict[1] += array2 (一旦您已经完成my_dict[1] = array1 )。

This is an expansion of the first answerer and my comment to the first answerer. 这是第一个答复者的扩展,也是我对第一个答复者的评论。

If I have the following arrays: 如果我有以下数组:

array1 = [1,2,3,4]
array2 = [2,6,27,1]
array3 = [6,1,2,3]
array4 = [7,1,2,6]
array5 = [8,2,1,6]
array6 = [3,4,1,1]

My dictionary is the following: 我的字典如下:

from collections import defaultdict
my_dict = defaultdict(list)

Now I will append the following arrays for different keys: 现在,我将为不同的键添加以下数组:

my_dict[1].append(array1)
my_dict[1].append(array2)
my_dict[1].append(array3)
my_dict[2].append(array4)
my_dict[3].append(array5)
my_dict[3].append(array6)

my_dict's contents my_dict的内容

{1:[[1,2,3,4],[2,6,27,1],[6,1,2,3]], 2:[[7,1,2,6]], 3:[[8,2,1,6],[3,4,1,1]] }

To get the array associated with key = 1 获取与key = 1关联的数组

foo = my_dict[1]
foo
[[1,2,3,4],[2,6,27,1],[6,1,2,3]]

Now to get the 3rd column of data: 现在获取数据的第三列:

import numpy as np #Need to use it to convert to array and float later on
foo = np.array(foo);# Have to convert it to an array
doo = foo[:,2]
doo
[3,27,2]

Now there may be situations where your array may be a mix of string and float or integer such that all elements in the array are string. 现在可能存在数组可能是字符串和浮点数或整数混合的情况,从而数组中的所有元素都是字符串。

foo 
[['1','2','3','4'],['2','6','27','1'],['6','1','2','3']]
foo = np.float32(foo)
foo
[[1.,2.,3.,4.],[2.,6.,27.,1.],[6.,1.,2.,3.]]

Conclusion: We can append arrays to a dictionary for a specific key using the defaultdict. 结论:我们可以使用defaultdict将数组追加到特定关键字的字典中。 Also subsets of the selected matrix can be extracted via normal column selection method. 同样,可以通过常规列选择方法提取所选矩阵的子集。 If the array associated with the key contains string elements, we can convert the data to numpy.float32, say or wherever we need to do computation with numbers. 如果与键关联的数组包含字符串元素,则可以将数据转换为numpy.float32,例如在需要进行数字计算的位置或任何需要转换的位置。

Thank you, Anthony of Sydney 谢谢悉尼的安东尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM