Python elegant assignment based on True/False values

后端 未结 11 1526
日久生厌
日久生厌 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 09:42

    To measure speeds:

    from time import clock
    a,b,c = True,False,False
    
    A,B,C,D,E,F,G,H = [],[],[],[],[],[],[],[]
    
    
    for j in xrange(30):
    
    
        te = clock()
        for i in xrange(10000):
            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')
        A.append(clock()-te)
    
    
    
        te = clock()
        for i in xrange(10000):
            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'
        B.append(clock()-te)
    
        #=====================================================================================
    
        li = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
        te = clock()
        for i in xrange(10000):
            name = li[a*4 + b*2 + c]
        C.append(clock()-te)
    
        #=====================================================================================
    
        nth = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
        te = clock()
        for i in xrange(10000):
            name = nth[(a and 4 or 0) | (b and 2 or 0) | (c and 1 or 0)]
        D.append(clock()-te)
    
    
        nth = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
        te = clock()
        for i in xrange(10000):
            name = nth[(a and 4 or 0) + (b and 2 or 0) + (c and 1 or 0)]
        E.append(clock()-te)
    
        #=====================================================================================
    
        values = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
        te = clock()
        for i in xrange(10000):
            name = values[( 4 if a else 0 )| ( 2 if b else 0 ) | ( 1 if c else 0 )]
        F.append(clock()-te)
    
    
        values = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first']
        te = clock()
        for i in xrange(10000):
            name = values[( 4 if a else 0 ) + ( 2 if b else 0 ) + ( 1 if c else 0 )]
        G.append(clock()-te)
    
        #=====================================================================================
    
        dic = {(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"}
        te = clock()
        for i in xrange(10000):
            name = dic[a,b,c]
        H.append(clock()-te)
    
    
    
    
    print min(A),'\n', min(B),'\n\n', min(C),'\n\n', min(D),'\n',min(E),'\n\n',min(F),'\n', min(G),'\n\n', min(H)
    

    Result

    0.0480533140385 
    0.0450973517584 
    
    0.0309056039245 
    
    0.0295291720037 
    0.0286550385594 
    
    0.0280122194301 
    0.0266760160858 
    
    0.0249769174574
    

提交回复
热议问题