字符串&列表&元组&字典之间互转
#!/usr/bin/env/python #-*-coding:utf-8-*- #Author:LingChongShi #查看源码Ctrl+左键 #数据类型之间的转换 Str=‘www.baidu.com‘ ‘‘‘一、str--->list‘‘‘ print(‘str-->list:‘,Str.split(‘.‘),type(Str.split())) ‘‘‘二、str--->tuple‘‘‘ print(‘str--->tuple‘,Str.partition(‘.‘),type(Str.partition(‘.‘))) ‘‘‘三、str--->dict‘‘‘ Str=‘{"name":"xiaoshao","age":10,"address":"xian"}‘ print(‘str--->dict:‘,eval(Str),type(eval(Str))) List= [‘www‘, ‘baidu‘, ‘com‘] ‘‘‘一、list--->str‘‘‘ print(‘list--->str:‘,‘.‘.join(List),type(‘.‘.join(List))) ‘‘‘二、list--->tuple‘‘‘ print(‘list--->tuple:‘,tuple(List),type(tuple(List))) ‘‘‘三、list--->dict‘‘‘ print(‘list--->dict:‘,dict(enumerate(List)),type(dict(enumerate(List)))) List1= [1,2,3] print(‘list--->dict:‘,dict(zip(List,List1)),type(dict(zip(List,List1)))) List2= [[‘www‘,1],[‘baidu‘,2],[‘com‘,3]] print(‘list--->dict:‘,dict(List2),type(dict(List2))) Tuple=(‘www‘, ‘baidu‘, ‘com‘) ‘‘‘一、tuple--->str‘‘‘ print(‘tuple--->str:‘,‘.‘.join(Tuple),type(‘.‘.join(Tuple))) ‘‘‘二、tuple--->list‘‘‘ print(‘tuple--->list:‘,list(Tuple),type(list(Tuple))) ‘‘‘三、tuple--->dict‘‘‘ print(‘tuple--->dict:‘,dict(enumerate(Tuple)),type(dict(enumerate(Tuple)))) Dict={‘name‘:‘xiaoshao‘,‘age‘:10,‘address‘:‘xian‘} ‘‘‘一、dict--->str‘‘‘ print(‘dict--->str:‘,str(Dict),type(str(Dict))) ‘‘‘二、dict--->list‘‘‘ print(‘dict--->list:‘,list(Dict.keys()),type(list(Dict.keys()))) print(‘dict--->list:‘,list(Dict.values()),type(list(Dict.values()))) ‘‘‘三、dict--->tuple‘‘‘ print(‘dict--->tuple:‘,tuple(Dict.keys()),type(tuple(Dict.keys()))) print(‘dict--->tuple:‘,tuple(Dict.values()),type(tuple(Dict.values())))
相关推荐
reallyr 2020-04-08
xiaoseyihe 2020-08-11
pythonxuexi 2020-07-30
leoaran 2020-07-21
littie 2020-07-19
zcabcd 2020-07-18
yjsflxiang 2020-07-04
liusarazhang 2020-06-28
银角大王 2020-06-25
leoaran 2020-06-22
Lexan 2020-06-15
liusarazhang 2020-06-14
89411051 2020-06-14
chinademon 2020-06-11
leoaran 2020-06-06
tengyuan 2020-06-03
shengnanonly 2020-06-03
joyjoy0 2020-06-02