[英]Changing specific values in a python dictionary
I have the following dictionary in python:我在 python 中有以下字典:
dict={('M1 ', 'V1'): 5,
('M1 ', 'V2'): 5,
('M1 ', 'V3'): 5,
('M1 ', 'V4'): 5,
('M2', 'V1'): 5,
('M2', 'V2'): 5,
('M2', 'V3'): 5,
('M2', 'V4'): 5,
('M3', 'V1'): 5,
('M3', 'V2'): 5,
('M3', 'V3'): 5,
('M3', 'V4'): 5}
For contextualization, "dict" is a matrix distance (('Source', 'Destination'): Value) for an optimization problem, and in conducting a sensitivity analysis, I want to make the distances from M1 so high that the model won't choose it.对于上下文化,“dict”是优化问题的矩阵距离(('Source','Destination'):值),并且在进行灵敏度分析时,我想使与 M1 的距离如此之高,以至于 model 不会'不要选择它。 Therefore, I want to get the python code to change the value of each line where M1 is a source.因此,我想获取 python 代码来更改以 M1 为源的每一行的值。
What you are doing here is you want to filter dictionary keys based on a value.你在这里做的是你想根据一个值过滤字典键。 the keys here are of tuple type.这里的键是元组类型。 so basically you need to iterate the keys and check if they have the needed value.所以基本上你需要迭代键并检查它们是否具有所需的值。
#let's get a list of your keys first
l = [] #a placeholder for the dict keys that has 'M1' in source
for k in dict.keys(): # iterate the keys
if k[0].strip() == 'M1': # 'M1' in the source node, strip to remove whitespaces if found
l.append(k) # a list of the keys that has 'M1' as a source
There's no way to directly access the items where part of the key tuple is M1
.无法直接访问部分键元组为M1
的项目。 You will need to loop through.您将需要循环。
d={
('M1', 'V1'): 5,
('M1', 'V2'): 5,
('M1', 'V3'): 5,
('M1', 'V4'): 5,
('M2', 'V1'): 5,
('M2', 'V2'): 5,
('M2', 'V3'): 5,
('M2', 'V4'): 5,
('M3', 'V1'): 5,
('M3', 'V2'): 5,
('M3', 'V3'): 5,
('M3', 'V4'): 5
}
for source, dest in d:
if source == 'M1':
d[(source, dest)] *= 10000
This will change d
in-place to:这会将d
就地更改为:
{('M1', 'V1'): 50000,
('M1', 'V2'): 50000,
('M1', 'V3'): 50000,
('M1', 'V4'): 50000,
('M2', 'V1'): 5,
('M2', 'V2'): 5,
('M2', 'V3'): 5,
('M2', 'V4'): 5,
('M3', 'V1'): 5,
('M3', 'V2'): 5,
('M3', 'V3'): 5,
('M3', 'V4'): 5}
Also, I'm assuming the key "M1 "
with a space is a typo.另外,我假设带空格的键"M1 "
是错字。 I've changed that to "M1"
, above.我已将其更改为上面的"M1"
。 Adjust as required.根据需要进行调整。
f = lambda src, dist : (dist * 100) if (src == 'M1 ') else dist
new_dict = {(src, dst): f(src, v) for (src, dst), v in dict.items()}
I don't love using python comprehensions for complex code (personally I think .map()
function calls are more readable, but this works. It has the benefit of being pure functional - no values are actually mutated, so you can preserve the original for use elsewhere if you so wish.我不喜欢对复杂代码使用 python 理解(我个人认为.map()
function 调用更具可读性,但这很有效。它具有纯函数的好处 - 实际上没有值发生变化,因此您可以保留原始值如果您愿意,可以在别处使用。
First off, dict
is a reserved keyword, so don't use that as the name of your dictionary.首先, dict
是一个保留关键字,所以不要将它用作字典的名称。
distances = {(...): ...,}
for source, destination in distances:
if source == "M1 ":
distances[(source, destination)] = 100000 # or something
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.