python字符串列表元组字典集合转
- 格式:pdf
- 大小:70.95 KB
- 文档页数:8
python字符串列表元组字典集合转
⼀.字符串str
1.字符串转化列表
s = 'hello python'
li = list(s)
print li
print type(s)print type(li)1234
5
结果
['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
3
2.字符串转化元组
s = 'hello python'
t = tuple(s)
print t
print type(s)print type(t)1234
5
结果
('h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n')
3
3.字符串转化集合
s = 'hello python'
set1 = set(s)
print set1
print type(s)print type(set1)1234
5
结果:集合是⽆序的数据类型,转化集合过程中相同元素只出现⼀次,⽐如“o“
set([' ', 'e', 'h', 'l', 'o', 'n', 'p', 't', 'y'])
3
4.字符串转化字典,需要借助eval函数或者exec函数
s = '{"name":"redhat","age":"10"}'d = eval(s)
print type(s)
print dprint type(d)1234
5
结果
{'age': '10', 'name': 'redhat'}
3
exec函数
s = '{"name":"redhat","age":"10"}'
print type(s)
exec('c=' +s)
print c,"查看c的内容"
print "查看c的类型",type(c)1234
5
结果
{'age': '10', 'name': 'redhat'} 查看c的内容
查看c的类型
3
⼆.列表list,
1.列表转化字符串
li=["hello",1,1+3j]
print type(li)
s=str(li)
print sprint type(s)1234
5
结果
['hello', 1, (1+3j)]
3
2.列表转化元组
li=["hello",1,1+3j]
print type(li)
t=tuple(li)
print tprint type(t)12
34
5
结果
('hello', 1, (1+3j))
3
3.列表转化集合
li=["hello",1,1+3j,1,"2","3","2",3]
print type(li)
s=set(li)
print sprint type(s)1234
5
结果:转换后集合⽆序,另外原列表中出现的相同的字符,没了,3是int型,’3’是字符串str型,两者不同
set([1, 3, (1+3j), '3', '2', 'hello'])
3
4.单个列表⽆法转化字典,两个可以借助zip实现
li1 = ['NAME', 'AGE', 'gender']
li2 = ['redhat', 10, 'M']
d= dict(zip(li1,li2))print d,type(d)123
4
结果{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'}
1
三.元组tuple
1.元组转化字符串
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
s=str(t)
print sprint type(s)1234
5
结果
('hello', 1, (1+3j), 1, '2', '3', '2', 3)
32.元组转化列表
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
li=list(t)
print liprint type(li)1234
5
3.元组转化集合
t=("hello",1,1+3j,1,"2","3","2",3)
s=set(t)
print sprint type(s)123
4
结果
set([1, 3, (1+3j), '3', '2', 'hello'])
2
4.元组转化字典和列表相同,两个可以借助zip函数
t1 = ('NAME', 'AGE', 'gender')
t2 = ('redhat', 10, 'M')
d= dict(zip(t1,t2))print d,type(d)123
4
结果{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'}
1
四.集合set
1.集合转化字符串str
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
string=str(s)print type(string)1234
5
结果
set([1, 2L, 3.1, (1+4j), 'hello'])
3
2.集合转化列表list
s={1,2L,3.1,1,"hello",1+4j}print s
print type(s)
li1=list(s)
print li1print type(li1)12345
6
结果:
set([1, 2L, 3.1, (1+4j), 'hello'])
[1, 2L, 3.1, (1+4j), 'hello']
4
4.集合转化元组
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
t=tuple(s)
print tprint type(t)12345
6
结果
set([1, 2L, 3.1, (1+4j), 'hello'])
(1, 2L, 3.1, (1+4j), 'hello')
4
4.集合转化字典
s1={1,2,3,4}
s2 = {"a","b","c"}
d=dict(zip(s1,s2))
print dprint type(d)1234
5
结果
{1: 'a', 2: 'c', 3: 'b'}
2
五.字典eict
1.字典转化字符串str
把字典的keys-vlaues⼀起转哈化d = dict(a=1,b=2,c=3)
print type(d)
s=str(d)print s,type(s)123
4
结果
2
只转化字典的keys
d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.keys())print s,type(s)123
4
结果
2
只转化字典的values
d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.values())print s,type(s)123
4
结果
2
2.字典转化列表list
字典转化列表默认情况下,转化的是kyes键
d = dict(a=1,b=2,c=3)
print type(d)
li=list(d)print li,type(li)123
4
结果
2
可以转化values
d = dict(a=1,b=2,c=3)
print type(d)