I have this code in python, when I print the last line, it is giving an output "11100101100". I'm expecting the output,"011100101100". Notice that the output starts with 1 and not 0. although the variable gamma_sum_list is a list containing 12 digits and its starts with 0. The function somehow deletes the first zero automatically. The following is the exact gamma_sum_list
Input:
[0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]
Expected Output:
011100101100
Actual Output :
11100101100
def convert(list)
res = int("".join(map(str,list)))
return res
print(convert(gamma_sum_list))
enter code here
CodePudding user response:
Your issue is caused by converting the result of the join
operation to an integer. Integers do not have leading zeroes. If you remove the int
function you'll get a string with the leading zero you're after.
gamma_sum_list = [0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]
def convert(my_list):
res = "".join(map(str,my_list))
return res
print(convert(gamma_sum_list))
Output:
011100101100
CodePudding user response:
Consider that:
>>> "".join(list(map(str, [0, 1])))
'01'
How would you convert '01'
to an integer? Well, its just 1
.
>>> int("".join(list(map(str, [0, 1]))))
1
So you probably want to not convert the string to an int
, just keep it as a str
. If your string is really a number, its probably a binary number.
CodePudding user response:
def convert(some_list):
res = "".join(map(str,some_list))
return res
gamma_sum_list = [0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]
print(convert(gamma_sum_list))
or
conv = lambda x: ''.join(map(str, x))
print(conv(gamma_sum_list))