一起学习网 一起学习网


Python字符串、元组、列表、字典互相转换的方法

网络编程 Python字符串、元组、列表、字典互相转换的方法 06-22

废话不多说了,直接给大家贴代码了,代码写的不好还去各位大侠见谅。

#-*-coding:utf-8-*- 
#1、字典
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
#字典转为字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'}
print type(str(dict)), str(dict)
#字典可以转为元组,返回:('age', 'name', 'class')
print tuple(dict)
#字典可以转为元组,返回:(7, 'Zara', 'First')
print tuple(dict.values())
#字典转为列表,返回:['age', 'name', 'class']
print list(dict)
#字典转为列表
print dict.values
#2、元组
tup=(1, 2, 3, 4, 5)
#元组转为字符串,返回:(1, 2, 3, 4, 5)
print tup.__str__()
#元组转为列表,返回:[1, 2, 3, 4, 5]
print list(tup)
#元组不可以转为字典
#3、列表
nums=[1, 3, 5, 7, 8, 13, 20];
#列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20]
print str(nums)
#列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20)
print tuple(nums)
#列表不可以转为字典
#4、字符串
#字符串转为元组,返回:(1, 2, 3)
print tuple(eval("(1,2,3)"))
#字符串转为列表,返回:[1, 2, 3]
print list(eval("(1,2,3)"))
#字符串转为字典,返回:<type 'dict'>
print type(eval("{'name':'ljq', 'age':24}"))

21行Python代码实现拼写检查器
引入大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供非常好的拼写检查,比如你输入speling,谷歌会马上返回spelling。下面是用21行pyt

基于Python Shell获取hostname和fqdn释疑
一直以来被Linux的hostname和fqdn(FullyQualifiedDomainName)困惑了好久,今天专门抽时间把它们的使用细节弄清了。一、设置hostname/fqdn在Linux系统内设置hostname

谈谈Python进行验证码识别的一些想法
用python加验证码为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章。我大体看了一下,主要方法有几类:一类是通过对图片进行处理,然后


编辑:一起学习网

标签:字典,字符串,列表,验证码,不可以