Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If elif else alternative
#11
(Apr-11-2017, 12:07 PM)buran Wrote:
in addition one could use dict comprehension to construct the dict

d = {k+1:10**k for k in range(5)}

If you can do that you don't need the dic. In the original code you just use n=10**integer_length.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#12
(Apr-12-2017, 08:06 AM)Ofnuts Wrote: If you can do that you don't need the dic. In the original code you just use n=10**integer_length.

Yeah, that's correct and I provided 2 such alternatives in my first post. Note that there is also catch all other case  (e.g. when integer_length>5).
However OP asks for a solution using dict and metulburr provided solution based on dict, which I changed further.
Reply
#13
I have the code returning values which are always above the maxValue which was my goal. I have now decided I would like the value returned to be as follows; 
maxValue = 12.345 axis max = 15
maxValue = 452.38 axis max = 500
maxValue = 7612.2 axis max = 8000
maxValue = 628 axis max = 650

How can I do this?

import math

maxValue = 12.345 # maximum data value
integerPart = str(maxValue).split('.')[0] # if maxValue is a decimal this selects the integer part, ie. left of decimal
integerLength = len(integerPart) # length of integerPart

factor = {k+1:10**(k-2) for k in range(6)} # equivalent to below, 6 represents the maxValue integer length

n = factor.get(integerLength, 10000) # returns a value from the dictionary above dependent on integerLength
maxValueAdd = maxValue + n + 0.1 # n allows for a value to be returned that is higher by the same factor
d = math.ceil(maxValueAdd) # calculates the upper bound and returns an integer
e = int(round(d,-(integerLength-3)))

print "Maximum value = {}".format(maxValue)
print "Axis maximum = {}".format(e)
Reply
#14
(Apr-12-2017, 08:02 AM)buran Wrote: @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]
Not only is it not Pythonic, it's inefficient. The whole point of a dictionary is that it's runtime is constant, but these comprehensions are linear both in time and space.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Twilio alternative jmair 3 3,908 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Pillow alternative? kucingkembar 4 879 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  Alternative for Cairosvg? Maryan 0 2,462 Oct-26-2020, 01:27 PM
Last Post: Maryan
  another alternative to np.interp evelynow 1 2,930 Aug-22-2019, 03:32 PM
Last Post: Larz60+
  Multithreading alternative MartinV279 1 2,796 Aug-01-2019, 11:41 PM
Last Post: scidam
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,244 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Array alternative oldcity 3 3,481 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