简体   繁体   English

OrderedDict中的元组

[英]Tuples within OrderedDict

I have the following OrderedDict 我有以下OrderedDict

activities = OrderedDict([('eating',(1,False)),('drinking',(2,True)),('breathing',(3,True)),('talking',(4,False)),('walking',(5,False))])

I want to be able to able to run a function to reverse the True/False part of a particular entry. 我希望能够运行一个函数来反转特定条目的True / False部分。

so far I have the following: 到目前为止,我有以下内容:

def updateactivity(activity):
    activities = OrderedDict([(k, (1,False)) if k == activity else (k, v) for k, v in activities.items()])

Which does well to switch the value to False. 将值切换为False效果很好。 Is there a more elegant way to simply reverse the True/False in either direction? 是否有一种更优雅的方法来简单地在任一方向上反转对/错?

Unpack each item's value into two variables/names then reconstruct with not . 将每个项目的值分解为两个变量/名称,然后使用not进行重构。

d = collections.OrderedDict()
for k, (a,b) in activities.items():
    d[k] = (a, not b)

or 要么

collections.OrderedDict((k,(a, not b)) for k, (a,b) in activities.items())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM