python字符串 列表 元组 字典 集合转
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python字符串 列表 元组 字典 集合转,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4819字,纯文字阅读大概需要7分钟。
内容图文
![python字符串 列表 元组 字典 集合转](/upload/InfoBanner/zyjiaocheng/618/a865205e96d84d4d96b32aa514fb7980.jpg)
一.字符串str
1.字符串转化列表
s = 'hello python'
li = list(s)
print li
print type(s)
print type(li)
- 1
- 2
- 3
- 4
- 5
结果
['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
<type 'str'>
<type 'list'>
- 1
- 2
- 3
2.字符串转化元组
s = 'hello python'
t = tuple(s)
print t
print type(s)
print type(t)
- 1
- 2
- 3
- 4
- 5
结果
('h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n')
<type 'str'>
<type 'tuple'>
- 1
- 2
- 3
3.字符串转化集合
s = 'hello python'
set1 = set(s)
print set1
print type(s)
print type(set1)
- 1
- 2
- 3
- 4
- 5
结果:集合是无序的数据类型,转化集合过程中相同元素只出现一次,比如“o“
set([' ', 'e', 'h', 'l', 'o', 'n', 'p', 't', 'y'])
<type 'str'>
<type 'set'>
- 1
- 2
- 3
4.字符串转化字典,需要借助eval函数或者exec函数
s = '{"name":"redhat","age":"10"}'
d = eval(s)
print type(s)
print d
print type(d)
- 1
- 2
- 3
- 4
- 5
结果
<type 'str'>
{'age': '10', 'name': 'redhat'}
<type 'dict'>
- 1
- 2
- 3
exec函数
s = '{"name":"redhat","age":"10"}'
print type(s)
exec('c=' +s)
print c,"查看c的内容"
print "查看c的类型",type(c)
- 1
- 2
- 3
- 4
- 5
结果
<type 'str'>
{'age': '10', 'name': 'redhat'} 查看c的内容
查看c的类型 <type 'dict'>
- 1
- 2
- 3
二.列表list,
1.列表转化字符串
li=["hello",1,1+3j]
print type(li)
s=str(li)
print s
print type(s)
- 1
- 2
- 3
- 4
- 5
结果
<type 'list'>
['hello', 1, (1+3j)]
<type 'str'>
- 1
- 2
- 3
2.列表转化元组
li=["hello",1,1+3j]
print type(li)
t=tuple(li)
print t
print type(t)
- 1
- 2
- 3
- 4
- 5
结果
<type 'list'>
('hello', 1, (1+3j))
<type 'tuple'>
- 1
- 2
- 3
3.列表转化集合
li=["hello",1,1+3j,1,"2","3","2",3]
print type(li)
s=set(li)
print s
print type(s)
- 1
- 2
- 3
- 4
- 5
结果:转换后集合无序,另外原列表中出现的相同的字符,没了,3是int型,’3’是字符串str型,两者不同
<type 'list'>
set([1, 3, (1+3j), '3', '2', 'hello'])
<type 'set'>
- 1
- 2
- 3
4.单个列表无法转化字典,两个可以借助zip实现
li1 = ['NAME', 'AGE', 'gender']
li2 = ['redhat', 10, 'M']
d= dict(zip(li1,li2))
print d,type(d)
- 1
- 2
- 3
- 4
结果
{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'} <type 'dict'>
- 1
三.元组tuple
1.元组转化字符串
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
s=str(t)
print s
print type(s)
- 1
- 2
- 3
- 4
- 5
结果
<type 'tuple'>
('hello', 1, (1+3j), 1, '2', '3', '2', 3)
<type 'str'>
- 1
- 2
- 3
2.元组转化列表
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
li=list(t)
print li
print type(li)
- 1
- 2
- 3
- 4
- 5
3.元组转化集合
t=("hello",1,1+3j,1,"2","3","2",3)
s=set(t)
print s
print type(s)
- 1
- 2
- 3
- 4
结果
set([1, 3, (1+3j), '3', '2', 'hello'])
<type 'set'>
- 1
- 2
4.元组转化字典和列表相同,两个可以借助zip函数
t1 = ('NAME', 'AGE', 'gender')
t2 = ('redhat', 10, 'M')
d= dict(zip(t1,t2))
print d,type(d)
- 1
- 2
- 3
- 4
结果
{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'} <type 'dict'>
- 1
四.集合set
1.集合转化字符串str
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
string=str(s)
print type(string)
- 1
- 2
- 3
- 4
- 5
结果
set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
<type 'str'>
- 1
- 2
- 3
2.集合转化列表list
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
li1=list(s)
print li1
print type(li1)
- 1
- 2
- 3
- 4
- 5
- 6
结果:
set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
[1, 2L, 3.1, (1+4j), 'hello']
<type 'list'>
- 1
- 2
- 3
- 4
4.集合转化元组
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
t=tuple(s)
print t
print type(t)
- 1
- 2
- 3
- 4
- 5
- 6
结果
set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
(1, 2L, 3.1, (1+4j), 'hello')
<type 'tuple'>
- 1
- 2
- 3
- 4
4.集合转化字典
s1={1,2,3,4}
s2 = {"a","b","c"}
d=dict(zip(s1,s2))
print d
print type(d)
- 1
- 2
- 3
- 4
- 5
结果
{1: 'a', 2: 'c', 3: 'b'}
<type 'dict'>
- 1
- 2
五.字典eict
1.字典转化字符串str
把字典的keys-vlaues一起转哈化
d = dict(a=1,b=2,c=3)
print type(d)
s=str(d)
print s,type(s)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
{'a': 1, 'c': 3, 'b': 2} <type 'str'>
- 1
- 2
只转化字典的keys
d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.keys())
print s,type(s)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
['a', 'c', 'b'] <type 'str'>
- 1
- 2
只转化字典的values
d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.values())
print s,type(s)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
[1, 3, 2] <type 'str'>
- 1
- 2
2.字典转化列表list
字典转化列表默认情况下,转化的是kyes键
d = dict(a=1,b=2,c=3)
print type(d)
li=list(d)
print li,type(li)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
['a', 'c', 'b'] <type 'list'>
- 1
- 2
可以转化values
d = dict(a=1,b=2,c=3)
print type(d)
li=list(d.values())
print li,type(li
- 1
- 2
- 3
- 4
结果
<type 'dict'>
[1, 3, 2] <type 'list'>
- 1
- 2
转化keys-values
d = dict(a=1,b=2,c=3)
print type(d)
li=list(d.iteritems())
print li,type(li)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
[('a', 1), ('c', 3), ('b', 2)] <type 'list'>
- 1
- 2
3.字典转化元组
同列表,默认情况下,转换keys键,其他方法同列表
d = dict(a=1,b=2,c=3)
print type(d)
t=tuple(d)
print t,type(t)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
('a', 'c', 'b') <type 'tuple'>
- 1
- 2
4.字典转集合set
默认强况下,转换keys键,其他转化同列表,元组
d = dict(a=1,b=2,c=3)
print type(d)
s1=set(d)
print s1,type(s1)
- 1
- 2
- 3
- 4
结果
<type 'dict'>
set(['a', 'c', 'b']) <type 'set'>
- 1
- 2
注意:
s是字符串str,s1是字典dict
s = "{'name':'root','passwd':'123'}"
s1 = {'name':'root','passwd':'123'}
print type(s)
print s,len(s)
print type(s1)
print s1,len(s1)
- 1
- 2
- 3
- 4
- 5
- 6
结果
<type 'str'>
{'name':'root','passwd':'123'} 30
<type 'dict'>
{'passwd': '123', 'name': 'root'} 2
- 1
- 2
- 3
- 4
内容总结
以上是互联网集市为您收集整理的python字符串 列表 元组 字典 集合转全部内容,希望文章能够帮你解决python字符串 列表 元组 字典 集合转所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。