码迷,mamicode.com
首页 > 编程语言 > 详细

Python - 列表与字典相互转换

时间:2016-07-03 15:53:56      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:列表、字典、字符串   类型转换   

字符串转列表

s = ‘abc‘

a = list(s)

[‘a‘,‘b‘,‘c‘]


列表转为字符串

‘‘.join(a)


字符串转换为元组

s=‘abc‘

t = tuple(s)


元组转换为字符串

‘‘.join(t)


列表转换为元组

l = [‘a‘,‘b‘,‘c‘]

tuple(l)


元组转换为列表

t = (‘a‘,‘b‘,‘c‘)

list(t)

[‘a‘,‘b‘,‘c‘]


字典转换为列表

dic={‘a‘:1,‘b‘:2}

dic.items()

[(‘a‘,1),(‘b‘,2)]

列表转换为字典

list1 = dic.items()

dict(list1)

本文出自 “少犟” 博客,请务必保留此出处http://xushaojie.blog.51cto.com/6205370/1795291

Python - 列表与字典相互转换

标签:列表、字典、字符串   类型转换   

原文地址:http://xushaojie.blog.51cto.com/6205370/1795291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!