Discuss / Python / 老师,最后一行reduce函数里面第二个参数说不是可iteration的参数呢

老师,最后一行reduce函数里面第二个参数说不是可iteration的参数呢

Topic source

def f(x,y): return x10+y def char2int(s): if s!='.': return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s] else: return '.' c=0 for i in s: if i=='.': break c=c+1 return reduce(f, (list(map(char2int, s)).remove('.')))/(10c)*

<em>TypeError: reduce() arg 2 must support iteration</em> 单独拿出来测试的时候是这个样子的:

L=(list(map(char2int, '12345.6789'))).remove('.') print(L) None 这样连着写L是None

L=list(map(char2int, '12345.6789')) print(L) [1, 2, 3, 4, 5, '.', 6, 7, 8, 9] L.remove('.') print(L) [1, 2, 3, 4, 5, 6, 7, 8, 9] 这样分开可以

想了一下大致上懂了,因为这样得到的并不是处理之后的那个list吧


  • 1

Reply