我有两个散列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。例如:
net8: cell24 {xo,x1} cell42 {xo,x1} net 18: cell11 {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我得到的只是每个网的最后价值。
你有什么想法吗?
发布于 2013-08-13 03:35:20
您面临的问题是,您正在为每个x0和x1值生成cell和x1值,但只为每个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']发布于 2013-08-12 18:26:02
试试这个:
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)https://stackoverflow.com/questions/18193761
复制相似问题