python字符串,元組,列表,字典互相轉(zhuǎn)換直接給大家上代碼實(shí)例

#-*-coding:utf-8-*-
#1、字典
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
#字典轉(zhuǎn)為字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'}
print type(str(dict)), str(dict)
#字典可以轉(zhuǎn)為元組,返回:('age', 'name', 'class')
print tuple(dict)
#字典可以轉(zhuǎn)為元組,返回:(7, 'Zara', 'First')
print tuple(dict.values())
#字典轉(zhuǎn)為列表,返回:['age', 'name', 'class']
print list(dict)
#字典轉(zhuǎn)為列表
print dict.values
#2、元組
tup=(1, 2, 3, 4, 5)
#元組轉(zhuǎn)為字符串,返回:(1, 2, 3, 4, 5)
print tup.__str__()
#元組轉(zhuǎn)為列表,返回:[1, 2, 3, 4, 5]
print list(tup)
#元組不可以轉(zhuǎn)為字典
#3、列表
nums=[1, 3, 5, 7, 8, 13, 20];
#列表轉(zhuǎn)為字符串,返回:[1, 3, 5, 7, 8, 13, 20]
print str(nums)
#列表轉(zhuǎn)為元組,返回:(1, 3, 5, 7, 8, 13, 20)
print tuple(nums)
#列表不可以轉(zhuǎn)為字典
#4、字符串
#字符串轉(zhuǎn)為元組,返回:(1, 2, 3)
print tuple(eval("(1,2,3)"))
#字符串轉(zhuǎn)為列表,返回:[1, 2, 3]
print list(eval("(1,2,3)"))
#字符串轉(zhuǎn)為字典,返回:<type 'dict'>
print type(eval("{'name':'ljq', 'age':24}"))
新聞名稱(chēng):python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article38/dpgdpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站改版、Google、網(wǎng)頁(yè)設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容