python 列表與元組的相互轉換

2021-09-29 01:58:07 字數 616 閱讀 9141

描述:將元祖轉換為列表,元組與列表是非常類似的,區別在於元組的元素值不能修改,元組是放在括號中,列表是放於方括號中。

方法:list( tup)

tup:要轉換為列表的元組

返回值:返回轉換後的列表。

示例:

if __name__ == "__main__":

tup_t = ('1213','agda','1e2df')

print tup_t

list_t = list(tup_t)

print list_t

輸出結果:

上面講的是將元組轉換成列表的操作函式,其實相應的還有將列表轉換成元組的函式

描述:將列表轉換成元組

方法:tuple(list)

list:要轉換成元組的列表

返回值:轉換後的元組

示例:

if __name__ == "__main__":

list_t = ['1213','agda','1e2df']

print list_t

tup_t = tuple(list_t)

print tup_t

輸出結果:

字典 元組 列表之間相互轉化 python

coding utf 8 1 字典 dict 字典轉為字串,返回 print type str dict str dict 字典可以轉為元組,返回 age name class print tuple dict 字典可以轉為元組,返回 7,zara first print tuple dict.va...

Python 列表 元組 字串相互轉換

我們直接上 字串和元組轉換成列表 example1 6666666 print example1 print type example1 list1 list example1 print list1 print type list1 example2 1 2,3 4,5 print example...

python 列表與字典相互轉換

1.2個列表轉換為字典 2.使用內建函式 zip 3.求乙個列表中所有資料型別的次數 encoding utf 8 l 1,2,s 1,23 1,2 set 1,2 b 2 d 先用字典定義列表中的資料型別 for i in l if isinstance i,str 判斷字串型別 d str 1 ...