在Python中将两个散列排序为一个

2024-06-16 08:39:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个哈希FP['NetC'],包含连接到特定网络的所有单元格,例如:

'net8': ['cell24', 'cell42'], 'net19': ['cell11', 'cell16', 'cell23', 'cell25', 'cell32', 'cell38']

FP['CellD_NM']包含每个单元的x1,x0坐标,例如:

{'cell4': {'Y1': 2.164, 'Y0': 1.492, 'X0': 2.296, 'X1': 2.576}, 'cell9': {'Y1': 1.895, 'Y0': 1.223, 'X0': 9.419, 'X1': 9.99}

我需要创建一个新的散列(或列表),为特定网络中的每个单元提供x0和x1。例如:

网络8: 单元格24{xo,x1} 单元格42{xo,x1} 净18: 单元格11{xo,x1} ... 你知道吗

这是我的密码

L1={}
L0={}
for net in FP['NetC']:

    for cell in FP['NetC'][net]:
            x1=FP['CellD_NM'][cell]['X1']
            x0=FP['CellD_NM'][cell]['X0']

            L1[net]=x1
            L0[net]=x0
print L1
print L0

我得到的只是每个网的最后一个值。你知道吗

你有什么想法吗?你知道吗


Tags: 网络l1netcell单元x1nmfp
2条回答

问题是您正在为每个cell生成x0x1值,但只为每个net分配结果。由于每个网络都有多个单元格,这将覆盖除最后一个值以外的所有单元格。你知道吗

看起来您需要的是嵌套字典,索引方式类似于X0[net][cell]。你可以这样做:

L0 = {}
L1 = {}
for net, cells in FP['NetC'].items(): # use .iteritems() if you're using Python 2
    L0[net] = {}
    L1[net] = {}
    for cell in cells:
        L0[net][cell] = FP['CellD_NM'][cell]['X0']
        L1[net][cell] = FP['CellD_NM'][cell]['X1']

试试这个:

for net in FP['NetC']:
    L1[net] = []
    L0[net] = []
    for cell in FP['NetC'][net]:
        x1=FP['CellD_NM'][cell]['X1']
        x0=FP['CellD_NM'][cell]['X0']

        L1[net].append(x1)
        L0[net].append(x0)

相关问题 更多 >