如何以最简单的方式将元组转换为字符串?例如。假设我有一个配对列表,例如:
[(1,"hi"),(True,False),(3,4)]
我想将第一对转换为 '1hi'
仅对于第一个元素:
l = [(1,"hi"),(True,False),(3,4)]
s = "".join(str(x) for x in l[0])
print s
要对所有元素执行此操作:
for t in l:
s = "".join(str(x) for x in t)
print s
给出:
1hi
TrueFalse
34
用这个:
>>> l = [(1,"hi"),(True,False),(3,4)]
>>> for i in l:
... print str(i[0])+str(i[1])
map(lambda e: ''.join(map(str,e)),
[(1,"hi"),(True,False),(3,4)])
使用map
:
>>> l = [(1,"hi"),(True,False),(3,4)]
>>> ''.join(map(str, l[0]))
'1hi'
>>> ''.join(map(str, l[1]))
'TrueFalse'
>>> ''.join(map(str, l[2]))
'34'