Python int值到字符串的元组

Python int值到字符串的元组,python,type-conversion,Python,Type Conversion,我需要在Python中进行如下转换: (1, 0, 7, 2, 3, 8, 4, 6, 5) 到 如何做到这一点?最简单的方法应该是使用a或a: 或 如果有字符串元组,则需要列表理解或生成器表达式将数字转换为字符串: t = ('a', 'b', 'c') ''.join(t) 在我的电脑上@georstef回答这个问题的时间只有2/3 >>> str(reduce(lambda x, y: x * 10 + y, (1, 0, 7, 2, 3, 8, 4, 6, 5)))

我需要在Python中进行如下转换:

(1, 0, 7, 2, 3, 8, 4, 6, 5)


如何做到这一点?

最简单的方法应该是使用a或a:

如果有字符串元组,则需要列表理解或生成器表达式将数字转换为字符串:

t = ('a', 'b', 'c')
''.join(t)

在我的电脑上@georstef回答这个问题的时间只有2/3

>>> str(reduce(lambda x, y: x * 10 + y, (1, 0, 7, 2, 3, 8, 4, 6, 5)))
'107238465'
下面比较其他几种方法

timeit ''.join([str(a) for a in t])
100000 loops, best of 3: 2.94 us per loop

timeit ''.join(str(a) for a in t)
100000 loops, best of 3: 3.39 us per loop

timeit str(reduce(lambda x, y: x * 10 + y, t))
100000 loops, best of 3: 2.06 us per loop

timeit str(t)[1: -1].replace(", ", "")
100000 loops, best of 3: 1.8 us per loop

timeit str(t).translate(None, "(), ")
1000000 loops, best of 3: 1.6 us per loop
reduce(lambda,b:a+str(b),(1,0,7,2,3,8,4,6,5),”)
Simpler:
”。连接(map(str,(1,0,7,2,3,8,4,6,5))
t = ('a', 'b', 'c')
''.join(t)
>>> str(reduce(lambda x, y: x * 10 + y, (1, 0, 7, 2, 3, 8, 4, 6, 5)))
'107238465'
timeit ''.join([str(a) for a in t])
100000 loops, best of 3: 2.94 us per loop

timeit ''.join(str(a) for a in t)
100000 loops, best of 3: 3.39 us per loop

timeit str(reduce(lambda x, y: x * 10 + y, t))
100000 loops, best of 3: 2.06 us per loop

timeit str(t)[1: -1].replace(", ", "")
100000 loops, best of 3: 1.8 us per loop

timeit str(t).translate(None, "(), ")
1000000 loops, best of 3: 1.6 us per loop