[英]Converting a list of base 10 integer into list of base 4 in python
First of all I am sorry if the title did not correct. 首先,对不起,标题不正确。 I am trying to build a program which convert a list of base 10 integer into its binary string.For example I have this list 我正在尝试构建一个程序,将基数为10的整数列表转换为其二进制字符串。例如,我有此列表
a = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
and I want to convert it into base 4. Perhaps it should look like this 我想将其转换为基数4。也许看起来像这样
b4 = [[00],[01],[02],...[33]]
Below is what I've wrote so far 以下是我到目前为止写的
def base10base4(pix,b):
pix = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
for i in range(pix):
digits = []
while pix > 0:
digits.insert(0, pix % b)
pix = pix // b
print(base10base4(pix[i], 4))
When I run this code it give me error like this 当我运行此代码时,它给我这样的错误
NameError: name 'pix' is not defined
I am still very new in Python. 我对Python还是很陌生。 Did I miss something or I need to change the whole code? 我错过了什么吗?还是需要更改整个代码? I am using Python 3.5 Thank you for the help 我正在使用Python 3.5谢谢您的帮助
This is a recursive function that returns a list of strings of base b representations of the integers in your list 这是一个递归函数,它返回列表中整数的基数b表示形式的字符串列表
def convertToBase(num,base):
q=num/base
r=num%base
if (q == 0):
return str(r)
else:
return convertToBase(q,base) + str(r)
pix = [0,3,15,56,12,34] #the list of numbers you want converted to base b
print [convertToBase(x,4) for x in pix]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.