0

如何以最简单的方式将元组转换为字符串?例如。假设我有一个配对列表,例如:

[(1,"hi"),(True,False),(3,4)] 

我想将第一对转换为 '1hi'

4

4 回答 4

2

仅对于第一个元素:

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
于 2013-11-10T05:13:35.640 回答
2

用这个:

>>> l = [(1,"hi"),(True,False),(3,4)]
>>> for i in l:
...     print str(i[0])+str(i[1])
于 2013-11-10T05:13:52.227 回答
2
map(lambda e: ''.join(map(str,e)), 
        [(1,"hi"),(True,False),(3,4)])
于 2013-11-10T05:22:00.420 回答
1

使用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'
于 2013-11-10T05:15:07.290 回答