Python 字符串、字典、列表、元组、集合之间的相互转换

一、字典
(1)字典转字符串
字典转字符串可以直接使用str函数
dict1 = {'s':12,"er":"io"}
str1 = str(dict1)
结果:
{'s': 12, 'er': 'io'}
利用json进行转换
import json
data1 = {'b': 789, 'c': 456, 'a': 123}
encode_json = json.dumps(data1)
print type(encode_json), encode_json

结果:
<type 'str'> {"a": 123, "c": 456, "b": 789}


(2)字典转元组
字典转元组可以直接使用tuple函数进行转换,默认转换的是key,如果要转换values需要指定
 dict1 = {'key1':'value1','key2':'value2','key3':'value3'}
tuple1 = tuple(dict1)
tuple2 = tuple(dict1.values())

结果:
('key3', 'key2', 'key1')
('value3', 'value2', 'value1')

(3)字典转列表list
dict1 = {'key1':'value1','key2':'value2','key3':'value3'}
list1 = list(dict1)
list2 = list(dict1.values())

结果:
['key3', 'key2', 'key1']
['value3', 'value2', 'value1']

二、元组
(1)元组转字符串
tuple1 = ("tuple1","tuple2","tuple3")
str1 = str(tuple1)
str2 = str(tuple1[0])
str3 = "".join(tuple1)
str4 = ",".join(tuple1)
str5 = tuple1.__str__()

结果:
"('tuple1', 'tuple2', 'tuple3')"
'tuple1'
'tuple1tuple2tuple3'
'tuple1,tuple2,tuple3'
"('tuple2', 2, 'tuple3')"

注意:在使用join将元组转换成字符串时,元组的元素必须都是字符串,不能含有整数
tuple2 = ("tuple2",2,"tuple3")
str2 = ",".join(tuple2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected string, int found

(2)元组转列表
元组转列表可以直接使用list函数进行转换
tuple1 = ('tuple1', 'tuple2', 'tuple3')
tuple2 = ('tuple2', 2, 'tuple3')
list1 = list(tuple1)
list2 = list(tuple2)

结果:
['tuple1', 'tuple2', 'tuple3']
['tuple2', 2, 'tuple3']

三、列表
(1)列表转字符串
列表转为字符串可以直接使用str或者是join
list1 = ["www","baidu","com"]
str1 = str(list1)
str2 = "".join(list1)
str3 = ".".join(list1)
结果:
"['list1', 1, 2, 3, 4, 'list2']"
'www.baidu.com'
注意:
在使用join转换时,列表元素应该都是字符
含有除字符以外的列表转字符串

list1 = ['www','baidu','com',12,34]
str1 = ".".join(map(str,list1))

结果:
'www.baidu.com.12.34'

(2)列表转元组
列表转元组可以直接使用tuple函数
list1 = ['www','baidu','com',12,34]
tuple1 =  ".".join(map(str,list1))
结果:
('www', 'baidu', 'com', 12, 34)

(3)一个列表不可以转字典,两个列表转字典
list1 = ['key1','key2','key3']
list2 = ['1','2','3']
dict(zip(list1,list2))
(4)#嵌套列表转字典
list3 = [['key1','value1'],['key2','value2'],['key3','value3']]
dict(list3)
(5)列表转集合(对列表元素去重)
list1 = [2,3,4,5,5,66,2,2]
s = set(list1)
print type(s)
print s
(6)列表、元组转字符串
list2 = ['a', 'a', 'b']
''.join(list2)

tup1 = ('a', 'a', 'b')
''.join(tup1)

四、字符串
(1)字符串转元组
str1 = "www.baidu.com"
tuple1 = tuple(str1)

结果:
('w', 'w', 'w', '.', 'b', 'a', 'i', 'd', 'u', '.', 'c', 'o', 'm')
根据指定的字符将字符串转为元组
str1 = "www.baidu.com"
tuple1 = tuple(str1.split('.'))
结果:
('www', 'baidu', 'com')
(2)字符串转列表
str1 = "www.baidu.com"
list1 = list(str1)
结果:
['w', 'w', 'w', '.', 'b', 'a', 'i', 'd', 'u', '.', 'c', 'o', 'm']
根据指定的字符将字符串进行拆分为列表
str1 = "www.baidu.com"
list1 = str1.split('.')
结果:
['www', 'baidu', 'com']

(3)字符串转字典
字符串转字典可以使用eval()函数,但是该函数在使用的过程中会遇到一些问题,比如会提示bool类型true、null没有定义
str2 = "{'key1':'valu1','key2':'value2'}"
dict2 = eval(str2)
结果:
{'key2': 'value2', 'key1': 'valu1'}
使用这个函数会出现一些错误
str1 = "{'key1':'value1','key2':true,'key3':null}"
dict1 = eval(str1)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'true' is not defined

出现这种错误的原因,是因为该函数无法识别true、null这些,解决方法是:
 global true
 global null

 true = 'true'
 null = 'null'
 dict1 = eval(str1)

 print dict1

结果:
{'key3': 'null', 'key2': 'true', 'key1': 'value1'}

对于多维字典的转换需要使用另外一个工具json

student = '{"grade":"three","message":{"name":"tom","age":12}}'
result = json.loads(student)
结果:
{u'grade': u'three', u'message': {u'age': 12, u'name': u'tom'}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值