python中str(字符串)、list(列表)、tuple(元组)、dict(字典)相互转化关系及字典键-值遍历
2018-02-02 17:19
1071 查看
#!/usr/bin/env python #coding=utf-8 def main(): strs = "this is a cjh's str" lst = ['this', 'is', 'a', "cjh's", 'list'] tpl = ('this', 'is', 'a', "cjh's", 'tuple') dct = {'name': 'cjh', 'age': '25', 'school': 'XDU'} print '>>>>>>>>>>>>>>>>Print Original Objects' print strs print lst print tpl print dct #转化为字符串 print '>>>>>>>>>>>>>>>>List、Tuple、Dictionary into a Str' print ' '.join(lst) print ' '.join(tpl) print ' '.join(dct.iterkeys()),#print默认换行,加逗号可以不换行 print ' '.join(dct.itervalues()) print #空的print表示换行 #转化为list print '>>>>>>>>>>>>>>>>Str、Tuple、Dictionary into a List' print list(strs) print list(tpl) print list(dct.iterkeys()) print list(dct.itervalues()) #转化为tuple print '>>>>>>>>>>>>>>>>Str、List、Dictionary into a Tuple' print tuple(strs) print tuple(lst) print tuple(dct.iterkeys()) print tuple(dct.itervalues()) #转化为dict print '>>>>>>>>>>>>>>>>Str into a Dictionary' #List、Tuple不能转化为Dictionary print eval("{'name':'cjh', 'age':25}") #字典的遍历 print ">>>>>>>>>>>>>>>>Dictionary's Traversal" #遍历键-值 print ' '.join(('%s:%s' % (key, value)) for key,value in dct.iteritems())#速度最快 #只遍历key print ' '.join(key for key in dct.iterkeys()) #只遍历values print ' '.join(value for value in dct.itervalues()) if __name__ == '__main__': main()
运行结果:
>>>>>>>>>>>>>>>>Print Original Objects
this is a cjh's str
['this', 'is', 'a', "cjh's", 'list']
('this', 'is', 'a', "cjh's", 'tuple')
{'age': '25', 'name': 'cjh', 'school': 'XDU'}
>>>>>>>>>>>>>>>>List、Tuple、Dictionary into a Str
this is a cjh's list
this is a cjh's tuple
age name school 25 cjh XDU
>>>>>>>>>>>>>>>>Str、Tuple、Dictionary into a List
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'c', 'j', 'h', "'", 's', ' ', 's', 't', 'r']
['this', 'is', 'a', "cjh's", 'tuple']
['age', 'name', 'school']
['25', 'cjh', 'XDU']
>>>>>>>>>>>>>>>>Str、List、Dictionary into a Tuple
('t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'c', 'j', 'h', "'", 's', ' ', 's', 't', 'r')
('this', 'is', 'a', "cjh's", 'list')
('age', 'name', 'school')
('25', 'cjh', 'XDU')
>>>>>>>>>>>>>>>>Str into a Dictionary
{'age': 25, 'name': 'cjh'}
>>>>>>>>>>>>>>>>Dictionary's Traversal
age:25 name:cjh school:XDU
age name school
25 cjh XDU
[Finished in 0.2s]
相关文章推荐
- python中str(字符串)、list(列表)、tuple(元组)、dict(字典)相互转化关系及字典键-值遍历
- 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)
- python 中list(列表),tupe(元组),str(字符串),dict(字典)之间的相互转换
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- Python数据结构 字符串,列表list,元组tuple,字典dict,容器
- Python 核心编程笔记_Chapter_2 Note_3_赋值与变量_字符串_列表list_元组tuple_字典dict
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- python中的字典(dict),列表(list),元组(tuple)
- Python中的列表(list),元组(Tuple),字典(Dict)和集合(Set)
- 003——Python内建数据结构列表(list)、元组(tuple)、字典(dict)、集合(set)
- Python中元组、列表、字典的遍历和相互转化
- Python中list(列表)、tuple(元组)、dict(字典)基本操作快速入门
- python中的列表(list),字典(dict),字符串(str)的基本操作
- python的列表(List),元组(Tuple),集合(Set),字典(Dict)
- python 字符串 元组 列表 字典 间的相互转化
- Python学习笔记———列表(list)、元组(tuple)、字典(dict)
- python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器
- Python中的列表(list),元组(Tuple),字典(Dict)和集合(Set)
- python列表list,元组tuple,字典dict