匹配两个列表值并追加到字典

时间:2020-04-22 12:40:36

标签: python-3.x dictionary for-loop

我有一个节点列表,其坐标格式为

MCoord = [[Node1,X,Y,Z],[Node2,X,Y,Z]...]

坐标:

MCoord  = [
    [1, 0, 0, 0],
    [2, 0, 1000, 1300],
    [3, 0, 2000, 2000],
    [4, 0, 3000, 2500],
    [5, 0, 4000, 3200],
    [6, 0, 5000, 4200],
    [7, 0, 6000, 6000],
    [8, 1000, 0, 0],
    [9, 1000, 1000, 1300],
    [10, 1000, 2000, 2000],
    [11, 1000, 3000, 2500],
    [12, 1000, 4000, 3200],
    [13, 1000, 5000, 4200],
    [14, 1000, 6000, 6000],
    [15, 2000, 0, 0],
    [16, 2000, 1000, 1300],
    // ...
    [27, 3500, 5000, 4200],
    [28, 3500, 6000, 6000]
]

我要存储具有相同X坐标的所有节点(及其坐标),并在键S1(具有相同X值的所有节点),S2,S3等下匹配对应的键值。

脚本:

SectionLocation = {'S1':0 , 'S2':1000 , 'S3':2000 , 'S4':3500}
SectionComplete = {'S1':0 , 'S2':0 , 'S3':0 , 'S4':0}

k = 0
for i in range(len(MCoord)):
    print(i)
    if MCoord[i][1] == SectionLocation[k]:
        keydic = get_key(SectionLocation[k])
        SectionComplete[keydic].append(MCoord[i])
        print(SectionComplete)
    else:
        k = k + 1
print(SectionComplete)

我似乎无法将新值附加到词典中。有什么建议吗?

期望的输出量:

SectionComplete = {
    'S1' : [
        [1, 0, 0, 0],
        [2, 0, 1000, 1300],
        [3, 0, 2000, 2000],
        [4, 0, 3000, 2500],
        [5, 0, 4000, 3200],
        [6, 0, 5000, 4200],
        [7, 0, 6000, 6000]
    ],
    'S2' : [
        [8, 1000, 0, 0],
        [9, 1000, 1000, 1300],
        [10, 1000, 2000, 2000],
        [11, 1000, 3000, 2500],
        [12, 1000, 4000, 3200],
        [13, 1000, 5000, 4200],
        [14, 1000, 6000, 6000]
    ],
    // ...
}

2 个答案:

答案 0 :(得分:0)

我相信这是您要实现的目标。但是如果我错了,你可以纠正我。

# your list of nodes and coordinates
MCoord = [[1, 0, 0, 0], 
          [2, 0, 1000, 1300], 
          [3, 0, 2000, 2000], 
          [4, 0, 3000, 2500], 
          [5, 0, 4000, 3200], 
          [6, 0, 5000, 4200], 
          [7, 0, 6000, 6000], 
          [8, 1000, 0, 0], 
          [9, 1000, 1000, 1300], 
          [10, 1000, 2000, 2000], 
          [11, 1000, 3000, 2500], 
          [12, 1000, 4000, 3200], 
          [13, 1000, 5000, 4200], 
          [14, 1000, 6000, 6000], 
          [15, 2000, 0, 0], 
          [16, 2000, 1000, 1300]]

# a dictionary mapping of section to its ID
SectionLocation = {0: 'S1', 1000: 'S2', 2000: 'S3', 3500: 'S4'}
# A dictionary of the grouped sections
SectionComplete = {'S1': [], 'S2': [], 'S3': [], 'S4': []}

# we don't need the index since python for loops take care of that for us
for node in MCoord:
    # grab the relevant section from the mapping
    section = SectionLocation[node[1]]

    # append it to that sections empty list
    SectionComplete[section].append(node)

print(SectionComplete)

在此处的示例SectionComplete = {'S1':0 , 'S2':0 , 'S3':0 , 'S4':0}中,您将dict的值初始化为int s,然后尝试追加到这些int。从本质上讲,这就像尝试做这样的事情。

my_int = 0
my_int.append(23)

这将不起作用,因为int没有append方法。

答案 1 :(得分:0)

MCoord = [[...]]

import numpy as np
array_of_coords = np.array((MCoord))

uniq_X = np.unique(array_of_coords[:,1])
group_by_X = [[array_of_coords[array_of_coords[:,1]==i,:] for i in uniq_X]]

list_of_keys = ["S"+str(i) for i in range(len(uniq_X))]

dictionary = dict(zip(list_of_keys, group_by_X[0]))
print(dictionary)

退出:

{'S0': array([[   1,    0,    0,    0],
       [   2,    0, 1000, 1300],
       [   3,    0, 2000, 2000],
       [   4,    0, 3000, 2500],
       [   5,    0, 4000, 3200],
       [   6,    0, 5000, 4200],
       [   7,    0, 6000, 6000]]), 'S1': array([[   8, 1000,    0,    0],
       [   9, 1000, 1000, 1300],
       [  10, 1000, 2000, 2000],
       [  11, 1000, 3000, 2500],
       [  12, 1000, 4000, 3200],
       [  13, 1000, 5000, 4200],
       [  14, 1000, 6000, 6000]]), 'S2': array([[  15, 2000,    0,    0],
       [  16, 2000, 1000, 1300]])}