新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python中怎么將元組、字典轉(zhuǎn)化為列表
python中將元組、字典轉(zhuǎn)化為列表的方法:

python中可以使用list()方法將元組或字典轉(zhuǎn)化為列表:
list()方法語(yǔ)法:
list( tup )
參數(shù)
tup -- 要轉(zhuǎn)換為列表的元組。
返回值
返回列表。
示例:
將元組轉(zhuǎn)換為列表:
#!/usr/bin/python # -*- coding: UTF-8 -*- aTuple = (123, 'xyz', 'zara', 'abc'); aList = list(aTuple) print "列表元素 : ", aList
輸出結(jié)果如下:
列表元素 : [123, 'xyz', 'zara', 'abc']
將字典轉(zhuǎn)換為列表的方法:
dit = {'name':'zxf',
'age':'22',
'gender':'male',
'address':'shanghai'}
# 將字典的key轉(zhuǎn)換成列表
lst = list(dit)
print(lst) # ['name', 'age', 'gender', 'address']
# 將字典的value轉(zhuǎn)換成列表
lst2 = list(dit.values())
print(lst2) # ['zxf', '22', 'male', 'shanghai']推薦學(xué)習(xí)《Python教程》。
文章名稱:創(chuàng)新互聯(lián)Python教程:python中怎么將元組、字典轉(zhuǎn)化為列表
URL地址:http://www.dlmjj.cn/article/djppdoj.html


咨詢
建站咨詢
