我有一个八进制数的列表,我想转换成十进制。以下是我的课堂,讲述我目前所做的:class Octal:
#Reads in the file into numberList, converting each line into an int.
def __init__(self):
list = []
file = open("Number Lists/random_numbers3.txt")
for line in file:
list.append(int(line))
self.numberList = list
file.close()
#Convert numberList to decimal
def dec_convert(self):
decimal = 0
decimalList = []
for line in self.numberList:
temp = str(line)
i = 0
while i < len(temp):
digit = int(temp[i])
item = (digit * (8 ** (len(temp) - i)))
decimal = decimal + item
i += 1
decimalList.append(decimal)
return decimalList
def get_list(self):
return self.numberList
我从一个文件里读入数字,这很好。但我不认为这个函数有效。它一直在跑,没有完成。在
它看起来很糟糕而且很难阅读,所以我想知道是否有一种更简单的方法将列表中的每个八进制数转换为十进制数?在