python3字符串和字典、集合、元组的相互转换

avatar 2020年3月13日18:20:45 评论 1,722 次浏览

字符串转字典

字符串转字典可以使用内置参数eval()函数,只能针对字符串双引号是字典格式的字符串

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = "{'python3':'ruby','java':'DevOps'}"
print(wulaoer,type(wulaoer))
wulaoer_dict = eval(wulaoer)  #这里也可以使用exec('wulaoer_dict='+wulaoer)
print(wulaoer_dict,type(wulaoer_dict))

输出结果:

{'python3':'ruby','java':'DevOps'} <class 'str'>
{'python3': 'ruby', 'java': 'DevOps'} <class 'dict'>

还可以使用ast模块

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
import ast
wulaoer = "{'python3':'ruby','java':'DevOps'}"
print(wulaoer,type(wulaoer))
wulaoer_dict = ast.literal_eval(wulaoer)
print(wulaoer_dict,type(wulaoer_dict))

输出结果:

{'python3':'ruby','java':'DevOps'} <class 'str'>
{'python3': 'ruby', 'java': 'DevOps'} <class 'dict'>

使用json模块把字符串转字典,这里要注意,由于 json 语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号。

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
import json
wulaoer = '{"python3":"ruby","java":"DevOps"}'
#wulaoer = "{'python3':'ruby','java':'DevOps'}"
print(wulaoer,type(wulaoer))
wulaoer_dict= json.loads(wulaoer)
print(wulaoer_dict,type(wulaoer_dict))

输出结果:

{"python3":"ruby","java":"DevOps"} <class 'str'>
{'python3': 'ruby', 'java': 'DevOps'} <class 'dict'>

字典转字符串

字典转成字符串可以使用字符串的内置函数str(),直接转成字符串.

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = {'python3':'ruby','java':'DevOps'}
print(wulaoer,type(wulaoer))
wulaoer_str= str(wulaoer)
print(wulaoer_dict,type(wulaoer_str))

输出结果:

{'python3': 'ruby', 'java': 'DevOps'} <class 'dict'>
{'python3': 'ruby', 'java': 'DevOps'} <class 'str'>

上面使用的是str()内置函数,也可以通过字典转成列表之后再转成字符串,不过这种方法一般很少在开发中使用。

字符串转集合

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = "123456"
print(wulaoer,type(wulaoer))
wulaoer_set= set(wulaoer)
print(wulaoer_set,type(wulaoer_set))

输出结果:

123456 <class 'str'>
{'4', '1', '5', '3', '2', '6'} <class 'set'>

集合转字符串

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = {'python3','ruby','java','DevOps'}
print(wulaoer,type(wulaoer))
wulaoer_str= str(wulaoer)
print(wulaoer_str,type(wulaoer_str))

输出结果:

{'ruby', 'DevOps', 'java', 'python3'} <class 'set'>
{'ruby', 'DevOps', 'java', 'python3'} <class 'str'>

集合转元组

集合转元组可以使用内置函数tuple()直接转换

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = {'DevOps', 'python3', 'java', 'ruby'}
print(wulaoer,type(wulaoer))
wulaoer_tuple= tuple(wulaoer)
print(wulaoer_tuple,type(wulaoer_tuple))

输出结果:

{'python3', 'java', 'ruby', 'DevOps'} <class 'set'>
('python3', 'java', 'ruby', 'DevOps') <class 'tuple'>

元组转集合

同上,元组转集合也可以使用set()函数直接转换

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = ('python3','ruby','java','DevOps')
print(wulaoer,type(wulaoer))
wulaoer_set= set(wulaoer)
print(wulaoer_set,type(wulaoer_set))

输出结果:

('python3', 'ruby', 'java', 'DevOps') <class 'tuple'>
{'DevOps', 'python3', 'java', 'ruby'} <class 'set'>

总结,以上所有转换方式都有一种直接转换,是根据数据类型的函数直接转换成对应的数据类型,字典除外。字典的元素比较特殊,其他数据类型的元素一样,直接转换可以使用自己的内置函数,想转成什么数据类型,就用相应数据类型的内置函数即可,列表内置函数是list(), 字典的内置函数是dict(),字符串内置函数是str(),数字有几个数据类型整数int(),浮点数float(),复数commplex(),如果其他类型转数字可以先转字符串,然后通过字符串在转数字类型。下面举个例子:

元组转整数

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer=('2','3','4')
wulaoer=[int(x) for x in wulaoer]
print(wulaoer)
#或
# wulaoer=('1','2')
# wolf=list(map(int,wulaoer))
# print(str(wolf))

输出结果:

[2, 3, 4]

map()中接收了一个int()函数,一个wulaoer列表对象,把对象中所有列表的元素转成整数。在举个例子:

#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
def wulaoer(x):
	return x*x
print(list(map(wulaoer, (1, 2, 3, 4, 5, 6, 7, 8, 9))))

输出结果:

[1, 4, 9, 16, 25, 36, 49, 64, 81]

map()传入了一个元组对象,然后调用了wulaoer函数,把元组的元素分别x*x,然后转成列表。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: