基本上我需要帮助创建一个函数来读取一个列表的给定参数,遍历列表的每个数字,检查它们并将它们的二进制值添加到不同的列表中。我正在尝试使用此代码,但它的工作方式与我认为的不相符。欢迎任何帮助:side参数用于帮助对二进制文件进行排序。我有两套,取决于哪一方面'列表中的数字是打开的,它们具有不同的二进制代码。
def bin_convert(upc, side):
bin_list = []
if side == 0:
for digit in upc:
if digit == 0:
bin_list.append(0001101)
elif digit == 1:
bin_list.append(0011001)
elif digit == 2:
bin_list.append(0010011)
elif digit == 3:
bin_list.append(0111101)
elif digit == 4:
bin_list.append(0100011)
elif digit == 5:
bin_list.append(0110001)
elif digit == 6:
bin_list.append(0101111)
elif digit == 7:
bin_list.append(0111011)
elif digit == 8:
bin_list.append(0110111)
elif digit == 9:
bin_list.append(0001011)
print bin_list
return bin_list
else:
for digit in upc:
if digit == 0:
bin_list.append(1110010)
elif digit == 1:
bin_list.append(1100110)
elif digit == 2:
bin_list.append(1101100)
elif digit == 3:
bin_list.append(1000010)
elif digit == 4:
bin_list.append(1011100)
elif digit == 5:
bin_list.append(1001110)
elif digit == 6:
bin_list.append(1010000)
elif digit == 7:
bin_list.append(1000100)
elif digit == 8:
bin_list.append(1001000)
elif digit == 9:
bin_list.append(1110100)
print bin_list
return bin_list