【问题标题】:Matching two lists values and appending to dictionary匹配两个列表值并附加到字典
【发布时间】:2020-04-22 12:40:36
【问题描述】:

我有一个节点列表及其表单坐标

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]
    ],
    // ...
}

【问题讨论】:

  • 这行if MCoord[i][1] == SectionLocation[k] 是否产生 KeyError?
  • 不,它没有。 SectionComplete[keydic].append(MCoord[i]) 似乎会产生错误。我的理解是 SectionComplete 是一本字典,因此我不能附加列表 Mcoord

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


【解决方案1】:

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

# 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 值初始化为ints,然后尝试附加到那些ints。本质上,这就像尝试做这样的事情。

my_int = 0
my_int.append(23)

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

【讨论】:

  • 这正是我正在尝试的。非常感谢你。我理解用 int 初始化 dict 并尝试追加的错误做法,非常感谢。
【解决方案2】:
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]])}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2020-03-29
    相关资源
    最近更新 更多