0

I'm running multiple floats through a function to generate a scientific notation. However, not all floats are put in at all times (it is randomised) therefore generating an error.

Input:

a0,hvt,at,lambd = signify(a0,hvt,at,lambd)

Function:

def signify(*args):
    rst = []

    for arg in args:
        if arg >= 100.0 or arg <= 0.01:
            arg = '{:.2e}'.format(arg)
            rst.append(arg)
    return rst

In other words, 'rst' does not always consist of four elements (thanks for pointing it out Singh).

Is someone willing to point me in the right direction?

1
  • Please add example input, output, and the error. And in general - You could either use dummy variable of you insist on receiving the whole 4, or just return a dict. its varias with your usecase Commented Dec 9, 2018 at 7:03

1 Answer 1

1

i don't think you quite understand what the error is. Can you post the error message?

I suspect you trying to assign a0,hvt,at,lambd = signify(a0,hvt,at,lambd) is the real culprit, what if "rst" returning from the function does not have 4 elements? That syntax on the left hand side forces the list on the right hand side to be unpacked exactly into 4 elements, and raises a ValueError: too many values to unpack (expected 4) in a mismatch.

try result = signify(a0,hvt,at,lambd) and check output.

Update:

If you want to only modify only some of the 4 terms but allow the rest to pass as-is, you Just need an else portion. Here is how you can think of the entire process.

def signify(*args):
    rst = []
    print(args)
    for arg in args:
        if arg >= 100.0 or arg <= 0.01:
            arg = '{:.2e}'.format(arg) #returns a string
            rst.append(arg)
        else:
            rst.append(arg) #take note that this else statement is the same as the last statement of if block
            #also note that args going through else block are not "strings" unlike the if block, which gives a string during ".format()"
    return rst

We can improve on this.

def signify(*args):
    rst = []
    print(args)
    for arg in args:
        if arg >= 100.0 or arg <= 0.01:
            arg = '{:.2e}'.format(arg)
        rst.append(arg) #note that you may want to typecast to string to maintain uniformity.
        #rst.append(str(arg))
    return rst

However, This essentially is the same as applying a function on all args. We can create a function that emphasizes this "working on 1 term" approach.

def signify_single(single_arg):
    if single_arg >= 100 or single_arg <= 0.01:
        return '{:.2e}'.format(single_arg)
    return single_arg #or str(single_arg)
a,b,c,d = (signify_single(x) for x in (101,202,303,40))

But that makes us realise this is just an if-else statement. They do not have to be ugly necessarily. (PS. That last line is a list comprehension.)

a,b,c,d = ('{:.2e}'.format(x)
          if (x >= 100 or x <= 0.01)
          else x #or str (x)
          for x in (101,202,303,40))

The condition can be tweaked around a little to give us a cleaner comprehension. Note that these can be written in a single line as well if you prefer.

a,b,c,d = (x if (0.01 < x < 100) else '{:.2e}'.format(x) for x in (101,202,303,40))

You can use any of the styles which looks the cleanest to you, Or explore and find something even better. Just apply it to your case like this.

a0,hvt,at,lambd = (x if (0.01 < x < 100) else '{:.2e}'.format(x) for x in (a0,hvt,at,lambd))
Sign up to request clarification or add additional context in comments.

4 Comments

You are correct, rst does not always consists of 4 elements. (will add this to the post) But with my limited experience I'm having trouble coming to the right solution (without going all-out with if statements:))
ok. What is your goal? You want all 4 terms all the time but some of them modified if they match the condition? then you just are missing an else statement that passes the terms as-is.
@Sumerechny Updated, is this what you're looking for?
amazing! Thanks, especially for the additional explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.