简体   繁体   English

如何将压缩列表中的键值对添加到多个子词典

[英]How to add key:value pairs from a zipped list to multiple sub-dictionaries

I am trying to assign lists of values to keys within a nested dictionary.我正在尝试将值列表分配给嵌套字典中的键。

My structure is dictionary = {key1: {key2: list}}我的结构是字典 = {key1: {key2: list}}

I create my dictionary using the following code我使用以下代码创建我的字典

surfDict = {}

for i in range(1, number_of_cells + 1):
        
surfDict["surf"+str(i)] = {}

In this case I have 5 cells so generate 5 key1s inside the main dictionary.在这种情况下,我有 5 个单元格,因此在主字典中生成 5 个 key1。

Next, I prepare the next level of the dictionary.接下来,我准备下一个级别的字典。 I have a list of frames, named framesList, which will serve as the key2s.我有一个名为framesList 的帧列表,它将作为key2s。 Then I have a list of self-contained valuelists, containing 3 values I am interested in.然后我有一个自包含值列表的列表,其中包含我感兴趣的 3 个值。

These two lists are arranged in order so if I zip them, I pair up what I want to be my key2:value pairs in the dictionary.这两个列表是按顺序排列的,所以如果我压缩它们,我会在字典中配对我想要的 key2:value 对。

My issue is that my framesList (newIntFrameslst) contains 5 repeats of 26 integers.我的问题是我的 framesList (newIntFrameslst)包含 5 个重复的 26 个整数。 So for example [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3...... etc..]例如 [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3......等..]

The list of my valuelists (surfValueslst) is structured like so [[x, y, z], [x, y, z], [x, y, z], [x, y, z], [x, y, z] etc....]我的值列表(surfValueslst)的结构如下 [[x, y, z], [x, y, z], [x, y, z], [x, y, z], [x, y, z] 等等....]

I want to add each integer from the framesList as the key2 for my valuelists and have all of those individual dictionaries contained within each key1.我想将framesList 中的每个整数添加为我的值列表的key2,并在每个key1 中包含所有这些单独的字典。

However, I only want one frame in each dictionary, with its corresponding valuelist.但是,我只想要每个字典中的一个框架,以及相应的值列表。 So for example {{surf1: {1: [x, y, z]}, {2: [x, y, z]}, {3: [x, y, z]}, {4: [x, y, z]} etc....}, {surf2: {1: [x, y, z]}, {2: [x, y, z]}, {3: [x, y, z]}, {4: [x, y, z]} etc....}...etc...}例如 {{surf1: {1: [x, y, z]}, {2: [x, y, z]}, {3: [x, y, z]}, {4: [x, y , z]} 等等..}, {surf2: {1: [x, y, z]}, {2: [x, y, z]}, {3: [x, y, z]}, {4: [x, y, z]} 等等....}...等等...}

To do this I have tried this code:为此,我尝试了以下代码:

for i in range(1, number_of_cells + 1):对于范围内的 i (1, number_of_cells + 1):

for frame, values in zip(newIntFrameslst, surfValueslst):

    if frame not in surfDict["surf"+str(i)]:

        surfDict["surf"+str(i)][frame] = values
    
    else:

        try:

          surfDict["surf"+str(i + 1)][frame] = values

        except:

            surfDict["surf"+str(number_of_cells)][frame] = values

However, this does not seem to work.但是,这似乎不起作用。 It gives me the correct set of valuelists in the surf1 dictionary.它为我提供了 surf1 字典中正确的值列表集。 However, the remaining 4 dictionaries all contain the last set of valuelists from my data.但是,其余 4 个字典都包含我的数据中的最后一组值列表。 I tried other things as well but this is as close as I got.我也尝试了其他事情,但这与我得到的一样接近。

Apologies if this seems very convoluted, I tried to give as much detail as possible to avoid being vague.抱歉,如果这看起来很复杂,我尽量提供尽可能多的细节以避免含糊不清。

I'm not sure I understand completely:我不确定我是否完全理解:

Here's the "outer" dictionary ( number_of_cells = 3 to make it more assessable):这是“外部”字典( number_of_cells = 3以使其更易于评估):

number_of_cells = 3
surfDict = {}
for i in range(1, number_of_cells + 1):
    surfDict["surf" + str(i)] = {}
surfDict = {'surf1': {}, 'surf2': {}, 'surf3': {}}

Here are examples for newIntFrameslst and surfValueslst (both smaller, for the same reason):以下是newIntFrameslstsurfValueslst示例(两者都较小,原因相同):

newIntFrameslst = [i for i in range(1, 8) for _ in range(number_of_cells)]
surfValueslst = [[i+j for j in range(3)] for i in range(0, number_of_cells * 21, 3)]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7]

[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17], [18, 19, 20],
 [21, 22, 23], [24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35], [36, 37, 38],
 [39, 40, 41], [42, 43, 44], [45, 46, 47], [48, 49, 50], [51, 52, 53], [54, 55, 56],
 [57, 58, 59], [60, 61, 62]]

Then this那么这个

for i, (frame, value) in enumerate(zip(newIntFrameslst, surfValueslst)):
    surfDict["surf" + str((i % number_of_cells) + 1)][frame] = value

leads to导致

surfDict = {
 'surf1': {1: [0, 1, 2], 2: [9, 10, 11], 3: [18, 19, 20], 4: [27, 28, 29], 5: [36, 37, 38], 6: [45, 46, 47], 7: [54, 55, 56]},
 'surf2': {1: [3, 4, 5], 2: [12, 13, 14], 3: [21, 22, 23], 4: [30, 31, 32], 5: [39, 40, 41], 6: [48, 49, 50], 7: [57, 58, 59]},
 'surf3': {1: [6, 7, 8], 2: [15, 16, 17], 3: [24, 25, 26], 4: [33, 34, 35], 5: [42, 43, 44], 6: [51, 52, 53], 7: [60, 61, 62]}
}

Is that what you are looking for?这就是你要找的吗? (This differs a bit from the expected result that you've described above. But I think your description isn't correct, because this kind of object isn't possible.) (这与您上面描述的预期结果略有不同。但我认为您的描述不正确,因为这种对象是不可能的。)

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

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