Python Forum

Full Version: If elif else alternative
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
What is a more Pythonic way of doing this? I've seen some examples using tuples and dictionaries but they don't seem to return the same varialbe.

if integerLength == 1:
    n = 1
elif integerLength == 2:
    n = 10
elif integerLength == 3:    
    n = 100
elif integerLength == 4:
    n = 1000
elif integerLength == 5:    
    n = 10000
else:
    n = 100000
What is the context? What are you doing with it.
two alternatives

>>> integerLength = 3
>>> n = 100000 if integerLength > 5 else 10**integerLength # using ternary expression
>>> n
1000
>>> n = 10**min(integerLength, 5)
>>> n
1000
also, for ternary expressions check this
Code in full;
import math

maxValue = 415.2 
integerPart = str(maxValue).split('.')[0] # selects the integer part
integerLength = len(integerPart)

if integerLength == 1:
    n = 1
elif integerLength == 2:
    n = 10
elif integerLength == 3:    
    n = 100
elif integerLength == 4:
    n = 1000
elif integerLength == 5:    
    n = 10000
else:
    n = 100000
    
maxValueAdd = float(maxValue) + n + 0.1 # n allows for a value to be returned that is higher by the same factor 
d = int(math.ceil(maxValueAdd)) # calculates the upper bound and returns an integer

print "Maximum value = {}".format(maxValue)
print "Y axis maximum = {}".format(d)
Quote:What is a more Pythonic way of doing this? I've seen some examples using tuples and dictionaries

It would be something along the lines of.
d = {
    1:1,
    2:10,
    3:100,
    4:1000,
    5:10000
}

if integerLength in d:
    n = d[integerLength]
else:
    n = 100000
Or if you wanted the else clause in the dict 
d = {
    0:100000,
    1:1,
    2:10,
    3:100,
    4:1000,
    5:10000
}

if intergerLength in d:
    n = d[interLength]
else:
    n = d[0]
dont quote me on that though, i literally just woke up and it takes a few for my brain to start  Think
based on metulburr's suggestion:


>>> d = {
   1:1,
   2:10,
   3:100,
   4:1000,
   5:10000
}
>>> n = d.get(integerLength, 100000)

in addition one could use dict comprehension to construct the dict

d = {k+1:10**k for k in range(5)}
(Apr-11-2017, 12:07 PM)in addition one could use dict comprehension to construct the dict Wrote: [ -> ]
d = {k+1:10**k for k in range(5)}

That's great, as potentially the range may be higher than the 100000 I've detailed.  Dance
More pythonic way and i would say more easy way to implement this is using dictionary and list comprehension :

dict = {'
       1':1,
       '2':10,
       '3':100,
       '4':1000,
       '5':10000,
       'rest':100000,
       }

n=raw_input() # any input

print dict['rest'] if len([dict[i] for i in dict if i==n] )==0 else [dict[i] for i in dict if i==n][0] # your result
It's almost unreadable.
@ankit - there is nothing pythonic in this

dict['rest'] if len([dict[i] for i in dict if i==n] )==0 else [dict[i] for i in dict if i==n][0]
Pages: 1 2