Jonathan Laliberte
Jonathan Laliberte

Reputation: 2725

Prepending zeros to a list

The story is as follows. The user inputs a number. Python converts the string into an int, and uses an algorithm to make a binary number. For example:

firstdecimal=input("Please enter your first denary number: ")

> 45

StrToInt = int(firstdecimal)
InputInt = ToBin(StrToInt)     # a working binary conversion function
print(InputInt)

> [1, 0, 1, 1, 0, 1]

What I need is that the function Addzero(x) is able to take the binary number list (InputInt), and add 0s to the start. If there are already 8 elements in the list, then it does not need to add a 0.

def Addzero(value):
    reverse = value[::-1] 
    if len(value) != 8:
        value.extend([0])
        if len(value) == 8:
            reverse = value[::-1]
    elif len(value) == 8:
        return

I couldn't find the code that lets me add the elements to the start, so I just implemented a reverse feature to circumvent it.

When i print the content of this function I get...

> None

Which is evidently not the intended result. I need the output. (a full 8 element list composed of 0's, 1's)

Like This:

> [0, 0, 1, 0, 1, 1, 0, 1]

Upvotes: 1

Views: 2430

Answers (3)

Nelewout
Nelewout

Reputation: 6554

This might work? I have not tested it myself, but I think it should be fine.

def add_zero(binary_list):
    if len(binary_list) < 8:
        zeros = [0] * (8 - len(binary_list))
        return zeros + binary_list
    else:
        return binary_list

Upvotes: 4

piggs_boson
piggs_boson

Reputation: 1007

Other solutions fit the bill I guess, but why not just use:

firstdecimal = input("Please enter your first denary number: ")

> 45

binaryStr = '{0:08b}'.format(firstdecimal)  # string, not a list

print(binaryStr)
> '00101101'

print(list(map(int, binaryStr))  # gives you the list
> [0, 0, 1, 0, 1, 1, 0, 1]

Note that 08b in the format string denotes that the argument firstdecimal is to be converted to a zero-padded 8-length binary string.

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

The reason that it returns None is simply because your return statement is lacking an expression:

elif len(value) == 8:
    return

Therefore return simply means: "We're done, stop executing this function, nothing should be returned".

and furthermore not all code paths will do a return statement anyway. Finally your AddZero seems to add only a single zero, which can be inefficient.

A better solution is probably:

def Addzero(value):
    return [0] * max(0,8-len(value)) + value

Here the expression [0]*n means that you repeat 0 n times in a list. So [0]*4 will result in [0,0,0,0]. Now the number of times we need to add this is max(0,8-len(value)). The max(0,...) is in fact not even necessary, so you can rewrite it as:

def Addzero(value):
    return [0] * (8-len(value)) + value

It means thus that the number of elements we have to short to generate an eight item list are constructed as a list of zero. And we add (append) with + the original list to it.

As @Later42 shows in his answer, you can make it more efficient, by doing a len check first, and in case it is equivalent to eight, return the list itself:

def Addzero(value):
    if(len(value) > 8) :
        return value
    else :
        return [0] * (8-len(value)) + value

Demo using Python's interactive shell:

$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def Addzero(value):
...     return [0] * (8-len(value)) + value
... 
>>> Addzero([1, 0, 1, 1, 0, 1])
[0, 0, 1, 0, 1, 1, 0, 1]

Upvotes: 6

Related Questions