1.字符串转换为元组
s = "Marry"
list(s)
2.字符串转换为列表
s = "Marry"
list(s)
3.列表转换为元组
l = "Marry"
list(l)
4.元组转换为列表
tu = "Marry"
list(tu)
5.两个列表转换为字典表
li_1=[k1,k2,k3]
li_2=[v2,v2,v3]
dict(zip(li_1,li_2))
6.嵌套列表转换为字典表,有两种方法,
>>>new_list= [['key1','value1'],['key2','value2'],['key3','value3']]
>>>dict(list)
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
或者这样:
>>>new_list= [['key1','value1'],['key2','value2'],['key3','value3']]
>>>new_dict = {}
>>> for i in new_list:
... new_dict[i[0]] = i[1] #字典赋值,左边为key,右边为value
...
>>> new_dict
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
7.字典表转换为元组
dic={'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
tuple(dic)
8.字典表转换为列表
#列表生成器li = [i for i in range(100)]
dic={'k1':'v1', 'k2':'v2','k3':'v3'}
[[i,dic[i]] for i in dic]