exu*_*ero 28 python if-statement boolean
我有一个我想要设置的变量,具体取决于三个布尔值中的值.最直接的方式是if语句后跟一系列elifs:
if a and b and c:
name = 'first'
elif a and b and not c:
name = 'second'
elif a and not b and c:
name = 'third'
elif a and not b and not c:
name = 'fourth'
elif not a and b and c:
name = 'fifth'
elif not a and b and not c:
name = 'sixth'
elif not a and not b and c:
name = 'seventh'
elif not a and not b and not c:
name = 'eighth'
Run Code Online (Sandbox Code Playgroud)
这有点尴尬,我想知道是否有更多的Pythonic方法来处理这个问题.想到了几个想法.
字典黑客:
name = {a and b and c: 'first',
a and b and not c: 'second',
a and not b and c: 'third',
a and not b and not c: 'fourth',
not a and b and c: 'fifth',
not a and b and not c: 'sixth',
not a and not b and c: 'seventh',
not a and not b and not c: 'eighth'}[True]
Run Code Online (Sandbox Code Playgroud)我把它称之为黑客,因为我并不太疯狂,因为我认为其中七个键是假的并且相互重叠.
和/或魔术
name = (a and b and c and 'first' or
a and b and not c and 'second' or
a and not b and c and 'third' or
a and not b and not c and 'fourth' or
not a and b and c and 'fifth' or
not a and b and not c and 'sixth' or
not a and not b and c and 'seventh' or
not a and not b and not c and 'eighth')
Run Code Online (Sandbox Code Playgroud)这是有效的,因为Python ands和ors返回要评估的最后一个值,但你必须知道这是为了理解这个奇怪的代码.
这三个选项都不令人满意.您有什么推荐的吗?
Cli*_*ler 50
您可以将a,b和c视为三位,当它们组合在一起形成0到7之间的数字.然后,您可以得到一个值数组['first','second',...'eight' ]并使用位值作为数组的偏移量.这只是两行代码(一行用于将位组装成0-7的值,一行用于查找数组中的值).
这是代码:
nth = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
nth[(a and 4 or 0) | (b and 2 or 0) | (c and 1 or 0)]
Run Code Online (Sandbox Code Playgroud)
Tim*_*ker 29
用dict怎么样?
name = {(True, True, True): "first", (True, True, False): "second",
(True, False, True): "third", (True, False, False): "fourth",
(False, True, True): "fifth", (False, True, False): "sixth",
(False, False, True): "seventh", (False, False, False): "eighth"}
print name[a,b,c] # prints "fifth" if a==False, b==True, c==True etc.
Run Code Online (Sandbox Code Playgroud)
Sve*_*ach 11
也许不是更好,但怎么样
results = ['first', 'second', 'third', 'fourth',
'fifth', 'sixth', 'seventh', 'eighth']
name = results[((not a) << 2) + ((not b) << 1) + (not c)]
Run Code Online (Sandbox Code Playgroud)
如果a,b,c真的是布尔值:
li = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
name = li[a*4 + b*2 + c]
Run Code Online (Sandbox Code Playgroud)
如果他们不是布尔人:
li = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
a,b,c = map(bool,(a,b,c))
name = li[a*4 + b*2 + c]
Run Code Online (Sandbox Code Playgroud)
克林特米勒的想法
| 归档时间: |
|
| 查看次数: |
3663 次 |
| 最近记录: |