我想做的是以下内容:
test_dict = dict(value = 1,
property = properties_list[value-1]
)
自然,我不能这样做,因为它向我抛出一个错误,指出名称“值”未定义。
您可以使用Python 3.8 中引入的赋值表达式(又名“海象运算符”)来实现。
properties_list = [42, 13]
test_dict = dict(value=(_:=1), property=properties_list[_-1])
print(test_dict) # -> {'value': 1, 'property': 42}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句