数字位数大于10时,Python在Huffman编码中出错

import heapq

f = [int(x) for x in input().split()]

T=[]

for i in range(len(f)):
    heapq.heappush(T,(f[i],str(i)))

while len(T)>1:
    a=heapq.heappop(T)
    b=heapq.heappop(T)
    heapq.heappush(T,(a[0]+b[0],'('+a[1]+' '+b[1]+')' ))

temp = 0
cost = 0
index= 0
for i in T[0][1]:
    if i == "(":
        temp = temp + 1
    elif i == ")":
        temp = temp - 1
    elif i == " ":
        continue
    else:
        cost = cost + (temp*f[int(i)])
        if i != "(" or ")" or " ":
            index*=10
            index+f[int(i)]


print(cost)

我试图使霍夫曼编码无前缀的成本代码。我的代码运行良好,但是当输入大于9时,会出错。我发现当f [int(i)]大于10时,它会出错,因为代码将其读取为1和0。如何解决此错误?

> 43 13 12 16 9 7
230#correct
> 13 5 11 7 15 1 14 20 10 12 9
425#wrong