您的位置:首页 > 编程语言 > Python开发

python中数组,元组,字典和字符串之间的转换

2015-11-23 09:53 651 查看

python中数组,元组,字典和字符串之间的转换

>>> mytuple = (1,2,3)
>>> print list(mytuple)           # Tuple to list
[1, 2, 3]
>>>
>>> mylist = [1,2,3]              # List to tuple
>>> print tuple(mylist)
(1, 2, 3)
>>>
>>> mylist2 = [ ('blue',5), ('red',3), ('yellow',7) ]
>>> print dict(mylist2)           # List to dictionnary
{'blue': 5, 'yellow': 7, 'red': 3}
>>>
>>> mystring = 'hello'
>>> print list(mystring)          # String to list
['h', 'e', 'l', 'l', 'o']
>>>
>>> mylist3 = ['w','or','ld']
>>> print ''.join(mylist3)        # List to string
world
>>>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: