1. 程式人生 > >python 第十四篇 列表 元組 字串 集合之間的轉換

python 第十四篇 列表 元組 字串 集合之間的轉換

#Author:zhang
#列表:list
#元組:tuple
#字串:str
#集合:set
#---------------------列表轉換元組 字串 集合--------------------
#列表轉換成元組
list1=['a','b','c','d']
tup=tuple(list1)
print(type(tup))
print(tup)

#列表轉換成字串
string=''.join(list1)
print(type(string))
print(string)


#列表轉換成集合
set1=set(list1)
print(type(set1))
print(set1)

#--------------------------元組 轉換成列表 字串 集合----------------
#元組轉換成列表
tuple1=('q','w','e','r')
list2=list(tuple1)
print(type(list2))
print(list2)

#元組轉換成字串
string1=''.join(tuple1)
print(type(string1))
print(string1)

#元組轉換成集合
set2=set(tuple1)
print(type(set2))
print(set2)


#-------------字串轉化成列表 元組  集合-------------------------
#字串轉化成列表
string2='wasd'
list3=list(string2)
print(type(list3))
print(list3)
#字串轉化成元組
tuple2=tuple(string2)
print(type(tuple2))
print(tuple2)
#字串轉化成集合
set3=set(string2)
print(type(set3))
print(set3)

#---------------集合 轉化成字串 列表  元組----------------------
#由於集合是無序的,所以轉化出來的列表元組字串產生的結果也是隨機的。
#宣告一個非空集合
my_set={'z','x','c','v'}
#宣告一個空集合
my_set1=set()
#集合轉化成字串
string3=''.join(my_set)
print(type(string3))
print(string3)
#集合轉化成列表
list4=list(my_set)
print(type(list4))
print(list4)
#集合轉化成元組
tuple3=tuple(my_set)
print(type(tuple3))
print(tuple3)