首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >字典的python布尔键

字典的python布尔键
EN

Stack Overflow用户
提问于 2013-04-28 04:59:25
回答 2查看 6.1K关注 0票数 3

我想使用一组布尔值作为字典的关键字,这样(weather == sunny == temp == want )将是11或True,True而(weather == sunny == weather == clothing )将是10,(weather == cloudy == weather ==clothing) True,False将是00 where where= {11:"shorts",10:"jeans",00:“夹克”}有什么方法可以做到吗?我假设它可能需要位操作,我正在尝试尽可能快地保持这一点,以获得操作时间。

EN

Stack Overflow用户

发布于 2013-04-28 05:03:20

您可以使用按位或(|)运算符:

代码语言:javascript
运行
复制
sunny = 0
cloudy = 1
cold = 2
clothing = { (cold|cloudy) :"shorts", cold:"jeans", sunny:"jacket"}

weather = something()

print(clothing[ weather & (cold|cloudy) ])

但是@BrenBarn推荐的元组版本更好。

票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16256818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档