我在做一个程序,输入是一个1或0的列表(或数组)(例如[0,0,1,0]),输出将等价于列表的二进制值(0010表示2)。你知道吗
这是代码
def binary_array_to_number(arr):
arr = []
binary_rep = "0b" #prefix of a binary representation
for x in arr: # iterating through each part of the list(input)
binary_rep += str(x) # concatenating each part of the list
return int(binary_rep) # return the respective number of the binary representation
不幸的是,我得到一个错误,这是下面提到的。你知道吗
因此,我想知道我错在哪里。你知道吗
您正在重置函数第一行中的输入
arr
,这意味着它将不关心输入是什么。循环中的字符串串联也可能是一个巨大的性能问题。你知道吗试试看
相关问题 更多 >
编程相关推荐