Python elegant assignment based on True/False values

后端 未结 11 1608
日久生厌
日久生厌 2021-01-31 09:32

I have a variable I want to set depending on the values in three booleans. The most straight-forward way is an if statement followed by a series of elifs:

if a a         


        
11条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 10:01

    Maybe not much better, but how about

    results = ['first', 'second', 'third', 'fourth', 
               'fifth', 'sixth', 'seventh', 'eighth']
    name = results[((not a) << 2) + ((not b) << 1) + (not c)]
    

提交回复
热议问题