Python字符串、元組、列表、字典互相轉(zhuǎn)換的方法
更新時間:2016年01月23日 20:49:27 作者:Ruthless
這篇文章主要介紹了Python字符串、元組、列表、字典互相轉(zhuǎn)換的方法的相關(guān)資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼了,代碼寫的不好還去各位大俠見諒。
#-*-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}"))
相關(guān)文章
python執(zhí)行等待程序直到第二天零點(diǎn)的方法
這篇文章主要介紹了python執(zhí)行等待程序直到第二天零點(diǎn)的方法,涉及Python等待程序的實現(xiàn)技巧,需要的朋友可以參考下2015-04-04Python 3.6 -win64環(huán)境安裝PIL模塊的教程
PIL功能非常強(qiáng)大,但API卻非常簡單易用。這篇文章主要介紹了Python 3.6 -win64環(huán)境安裝PIL模塊的教程,需要的朋友可以參考下2019-06-06在python中使用requests 模擬瀏覽器發(fā)送請求數(shù)據(jù)的方法
今天小編就為大家分享一篇在python中使用requests 模擬瀏覽器發(fā)送請求數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python requests包的request()函數(shù)中的參數(shù)-params和data的區(qū)別介紹
這篇文章主要介紹了python requests包的request()函數(shù)中的參數(shù)-params和data的區(qū)別介紹,具有很好參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05