Posts: 16
Threads: 6
Joined: Mar 2017
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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
|
Posts: 12,030
Threads: 485
Joined: Sep 2016
What is the context? What are you doing with it.
Posts: 8,160
Threads: 160
Joined: Sep 2016
two alternatives
1 2 3 4 5 6 7 |
>>> integerLength = 3
>>> n = 100000 if integerLength > 5 else 10 * * integerLength
>>> n
1000
>>> n = 10 * * min (integerLength, 5 )
>>> n
1000
|
also, for ternary expressions check this
Posts: 16
Threads: 6
Joined: Mar 2017
Apr-11-2017, 11:55 AM
(This post was last modified: Apr-11-2017, 12:02 PM by brocq_18.)
Code in full;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import math
maxValue = 415.2
integerPart = str (maxValue).split( '.' )[ 0 ]
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
d = int (math.ceil(maxValueAdd))
print "Maximum value = {}" . format (maxValue)
print "Y axis maximum = {}" . format (d)
|
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-11-2017, 12:00 PM
(This post was last modified: Apr-11-2017, 12:00 PM by metulburr.)
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
Recommended Tutorials:
Posts: 8,160
Threads: 160
Joined: Sep 2016
Apr-11-2017, 12:07 PM
(This post was last modified: Apr-11-2017, 12:29 PM by buran.)
based on metulburr's suggestion:
1 2 3 4 5 6 7 8 |
>>> 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
1 |
d = {k + 1 : 10 * * k for k in range ( 5 )}
|
Posts: 16
Threads: 6
Joined: Mar 2017
(Apr-11-2017, 12:07 PM)in addition one could use dict comprehension to construct the dict Wrote:
1 |
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.
Posts: 11
Threads: 1
Joined: Mar 2017
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
Posts: 2,953
Threads: 48
Joined: Sep 2016
Posts: 8,160
Threads: 160
Joined: Sep 2016
@ ankit - there is nothing pythonic in this
1 |
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 ]
|
|