【发布时间】:2021-03-03 20:08:39
【问题描述】:
有必要形成一个字典作为参数,其中一组字典。在这些字典中有一个名为“reverse”的键。默认情况下,此键设置为 False。添加字典时,我会根据某些条件更改“反向”键的值。但由于某种原因,在一般源语词汇中,“反向”的意思只变成了真理报。
为什么意思随意变化,在什么地方?
# -*- coding: utf-8 -*-
struct_state_devices = None
dict_gate = {"state_gate": {},
"position": {"state": "", "stop": False},
"reverse": False,
}
test_list = [{"device_code": "1111", "reverse": False}, {"device_code": "2222", "reverse": True}]
if struct_state_devices is None:
struct_state_devices = dict()
for dev in test_list:
struct_state_devices[dev["device_code"]] = dict_gate # добавление словаря устройства
print("before: " + str(struct_state_devices))
for dev in test_list:
if dev["reverse"] is True:
struct_state_devices[dev["device_code"]]["reverse"] = True
# print(self.struct_state_devices[dev.device_code]['reverse'])
print("after: " + str(struct_state_devices))
输出:
before: {'1111': {'state_gate': {}, 'position': {'state': '', 'stop': False}, 'reverse': False}, '2222': {'state_gate': {}, 'position': {'state': '', 'stop': False}, 'reverse': False}}
after: {'1111': {'state_gate': {}, 'position': {'state': '', 'stop': False}, 'reverse': True}, '2222': {'state_gate': {}, 'position': {'state': '', 'stop': False}, 'reverse': True}}
【问题讨论】:
标签: python dictionary boolean