Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If elif else alternative
#1
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
Reply
#2
What is the context? What are you doing with it.
Reply
#3
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
Reply
#4
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)
Reply
#5
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
Recommended Tutorials:
Reply
#6
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)}
Reply
#7
(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
Reply
#8
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
Reply
#9
It's almost unreadable.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
@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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Twilio alternative jmair 3 3,886 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Pillow alternative? kucingkembar 4 864 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  Alternative for Cairosvg? Maryan 0 2,448 Oct-26-2020, 01:27 PM
Last Post: Maryan
  another alternative to np.interp evelynow 1 2,910 Aug-22-2019, 03:32 PM
Last Post: Larz60+
  Multithreading alternative MartinV279 1 2,774 Aug-01-2019, 11:41 PM
Last Post: scidam
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,237 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Array alternative oldcity 3 3,470 Oct-01-2018, 10:03 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020