如何把一个迭代对象转换为列表,元组,字符串???
python提供了三个内置函数来实现:
list([iterable])
把可迭代对象转换为列表
tuple([iterable])
把可迭代对象转换为元祖
str(obj) 把对象转换为字符串
>>> ss = "Hi,What's your name?"
>>> ss=list(ss)
>>> ss
['H', 'i', ',', 'W', 'h', 'a', 't', "'", 's', ' ', 'y', 'o', 'u', 'r', ' ', 'n', 'a', 'm', 'e', '?']
>>>
>>> tuple1=(1,2,3,4)
>>> ss=list(tuple1)
>>> ss
[1, 2, 3, 4]
>>> list1=[1,2,3,4]
>>> ss = str(list1)
>>> ss
'[1, 2, 3, 4]'
>>> tt=tuple(list1)
>>> tt
(1, 2, 3, 4)
>>>