python字符串 列表 元组 字典 集合的相互转化

29 篇文章 0 订阅

一.字符串str

1.字符串转化列表
s = 'hello python'
li = list(s)
print li
print type(s)
print type(li)

结果

['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
<type 'str'>
<type 'list'>
2.字符串转化元组
s = 'hello python'
t = tuple(s)
print t
print type(s)
print type(t)

结果

('h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n')
<type 'str'>
<type 'tuple'>
3.字符串转化集合
s = 'hello python'
set1 = set(s)
print set1
print type(s)
print type(set1)

结果:集合是无序的数据类型,转化集合过程中相同元素只出现一次,比如“o“

set([' ', 'e', 'h', 'l', 'o', 'n', 'p', 't', 'y'])
<type 'str'>
<type 'set'>
4.字符串转化字典,需要借助eval函数或者exec函数
s = '{"name":"redhat","age":"10"}'
d = eval(s)
print type(s)
print d
print type(d)

结果

<type 'str'>
{'age': '10', 'name': 'redhat'}
<type 'dict'>

exec函数

s = '{"name":"redhat","age":"10"}'
print type(s)
exec('c=' +s)
print c,"查看c的内容"
print  "查看c的类型",type(c)

结果

<type 'str'>
{'age': '10', 'name': 'redhat'} 查看c的内容
查看c的类型 <type 'dict'>

二.列表list,

1.列表转化字符串
li=["hello",1,1+3j]
print type(li)
s=str(li)
print s
print type(s)

结果

<type 'list'>
['hello', 1, (1+3j)]
<type 'str'>
2.列表转化元组
li=["hello",1,1+3j]
print type(li)
t=tuple(li)
print t
print type(t)

结果

<type 'list'>
('hello', 1, (1+3j))
<type 'tuple'>
3.列表转化集合
li=["hello",1,1+3j,1,"2","3","2",3]
print type(li)
s=set(li)
print s
print type(s)

结果:转换后集合无序,另外原列表中出现的相同的字符,没了,3是int型,’3’是字符串str型,两者不同

<type 'list'>
set([1, 3, (1+3j), '3', '2', 'hello'])
<type 'set'>
4.单个列表无法转化字典,两个可以借助zip实现
li1 = ['NAME', 'AGE', 'gender']
li2 = ['redhat', 10, 'M']
d= dict(zip(li1,li2))
print d,type(d)

结果

{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'} <type 'dict'>

三.元组tuple

1.元组转化字符串
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
s=str(t)
print s
print type(s)

结果

<type 'tuple'>
('hello', 1, (1+3j), 1, '2', '3', '2', 3)
<type 'str'>
2.元组转化列表
t=("hello",1,1+3j,1,"2","3","2",3)
print type(t)
li=list(t)
print li
print type(li)
3.元组转化集合
t=("hello",1,1+3j,1,"2","3","2",3)
s=set(t)
print s
print type(s)

结果

set([1, 3, (1+3j), '3', '2', 'hello'])
<type 'set'>
4.元组转化字典和列表相同,两个可以借助zip函数
t1 = ('NAME', 'AGE', 'gender')
t2 = ('redhat', 10, 'M')
d= dict(zip(t1,t2))
print d,type(d)

结果

{'gender': 'M', 'AGE': 10, 'NAME': 'redhat'} <type 'dict'>

四.集合set

1.集合转化字符串str
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
string=str(s)
print type(string)

结果

set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
<type 'str'>
2.集合转化列表list
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
li1=list(s)
print li1
print type(li1)

结果:

set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
[1, 2L, 3.1, (1+4j), 'hello']
<type 'list'>
4.集合转化元组
s={1,2L,3.1,1,"hello",1+4j}
print s
print type(s)
t=tuple(s)
print t
print type(t)

结果

set([1, 2L, 3.1, (1+4j), 'hello'])
<type 'set'>
(1, 2L, 3.1, (1+4j), 'hello')
<type 'tuple'>
4.集合转化字典
s1={1,2,3,4}
s2 = {"a","b","c"}
d=dict(zip(s1,s2))
print d
print type(d)

结果

{1: 'a', 2: 'c', 3: 'b'}
<type 'dict'>

五.字典eict

1.字典转化字符串str

把字典的keys-vlaues一起转哈化

d = dict(a=1,b=2,c=3)
print type(d)
s=str(d)
print s,type(s)

结果

<type 'dict'>
{'a': 1, 'c': 3, 'b': 2} <type 'str'>

只转化字典的keys

d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.keys())
print s,type(s)

结果

<type 'dict'>
['a', 'c', 'b'] <type 'str'>

只转化字典的values

d = dict(a=1,b=2,c=3)
print type(d)
s=str(d.values())
print s,type(s)

结果

<type 'dict'>
[1, 3, 2] <type 'str'>
2.字典转化列表list

字典转化列表默认情况下,转化的是kyes键

d = dict(a=1,b=2,c=3)
print type(d)
li=list(d)
print li,type(li)

结果

<type 'dict'>
['a', 'c', 'b'] <type 'list'>

可以转化values

d = dict(a=1,b=2,c=3)
print type(d)
li=list(d.values())
print li,type(li

结果

<type 'dict'>
[1, 3, 2] <type 'list'>

转化keys-values

d = dict(a=1,b=2,c=3)
print type(d)
li=list(d.iteritems())
print li,type(li)

结果

<type 'dict'>
[('a', 1), ('c', 3), ('b', 2)] <type 'list'>
3.字典转化元组

同列表,默认情况下,转换keys键,其他方法同列表

d = dict(a=1,b=2,c=3)
print type(d)
t=tuple(d)
print t,type(t)

结果

<type 'dict'>
('a', 'c', 'b') <type 'tuple'>
4.字典转集合set

默认强况下,转换keys键,其他转化同列表,元组

d = dict(a=1,b=2,c=3)
print type(d)
s1=set(d)
print s1,type(s1)

结果

<type 'dict'>
set(['a', 'c', 'b']) <type 'set'>
注意:

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)

结果

<type 'str'>
{'name':'root','passwd':'123'} 30
<type 'dict'>
{'passwd': '123', 'name': 'root'} 2
  • 23
    点赞
  • 111
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python编程语言中,有四种常用的集合数据类型,它们分别是列表(List)、元组(Tuple)、字典(Dictionary)和集合(Set)。列表是一种有序和可更改的集合,允许重复的成员。元组是一种有序且不可更改的集合,也允许重复的成员。字典是一个无序、可变且有索引的集合,其中没有重复的成员。而集合是一种无序和无索引的集合,没有重复的成员。这四种集合数据类型在Python中都有着不同的特点和用途。 在Python中,列表元组字符串都属于序列,它们的元素都可以通过编号进行访问。而字典则属于映射,其中的元素是以键值对的形式存在的。集合则不属于序列或映射中的任何一种,它是一种独立的数据类型,用于存储一组不重复的元素。 总结起来,Python中的列表(List)、元组(Tuple)、字典(Dictionary)和集合(Set)都是常用的集合数据类型,它们各自有着不同的特点和用途。列表是有序和可更改的集合元组是有序且不可更改的集合字典是无序、可变且有索引的集合集合是无序和无索引的集合。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [浅谈python四种集合数据类型—【列表元组集合字典】](https://blog.csdn.net/QWERTYzxw/article/details/121479048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python 元组集合字典](https://download.csdn.net/download/weixin_38603219/13752298)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值