2 回答
TA贡献1825条经验 获得超6个赞
如果我正确理解您的问题,您可以执行以下操作(在线尝试):
binary_list = [0, 0, 0, 0, 0, 1, 1, 10, 10, 11, 100, 100, 11, 10, 0]
quaternary_list = [-11, -33, -22, -132, -220, -310]
octal_list = [62, -220, -36, 5, 0, 1, -12]
def list_to_decimal(lst, base):
return [int(str(item), base) for item in lst]
print(list_to_decimal(binary_list, 2)) # => [0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 3, 2, 0]
print(list_to_decimal(quaternary_list, 4)) # => [-5, -15, -10, -30, -40, -52]
print(list_to_decimal(octal_list, 8)) # => [50, -144, -30, 5, 0, 1, -10]
它使用一个list_to_decimal接受列表lst和基数的函数工作base,然后使用列表推导式将 的每个元素解释lst为 的数量base。
这是否回答你的问题?
TA贡献1785条经验 获得超8个赞
你可以这样做。
binary_list = [0, 0, 0, 0, 0, 1, 1, 10, 10, 11, 100, 100, 11, 10, 0]
quaternary_list = [-11, -33, -22, -132, -220, -310]
octal_list = [62, -220, -36, 5, 0, 1, -12]
binary_list_do_dec = [int(str(i), 2) for i in binary_list]
quaternary_list_do_dec = [int(str(i), 4) for i in quaternary_list]
octal_list_do_dec = [int(str(i), 8) for i in octal_list]
print(binary_list_do_dec)
print(quaternary_list_do_dec)
print(octal_list_do_dec)
输出看起来像:
[0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 3, 2, 0]
[-5, -15, -10, -30, -40, -52]
[50, -144, -30, 5, 0, 1, -10]
添加回答
举报