python 列表,字典,元组,字符串,QuerySet之间的相互转换
1. 列表转换成字典
list1 = ['key1','key2','key3']
list2 = ['value1','value2']
dict1 = zip(list1,list2) # dict(dict1)={'key1':'value1','key2':'value2'}
2.字典转换为列表
dict1 = {'key1':'value1','key2':'value2'}
list1 = list(dict1) #list1 = ['key1','key2']
list1 = list(dict1.values()) #list1 = ['value1','value2']
3.列表转换为元组
list2 = ['value1','value2']
data_tuple = tuple(list2)
4.元组转换为列表
tuple1 = (1,2,3)
list1 = list(tuple1)
5.列表转换为字符串
list1 = ['key1','key2']
','.join(list1) key1,key2
6.字符串转换为列表
str_date ='12345'
list1 = list(str_date) #[1,2,3,4,5]
7.QuerySet转换为列表
list1 = Object.objects.values()
list1 = list(QuerySet)
8.字典转换为元组
dict1 = {'key1':'value1','key2':'value2'}
tuple(dict1) # ('key1','key2')
9.QuerySet转换为字典
model_to_dict(QuerySet)
Object.objects.get(id=id).__dict__
生命很短,请让生活更精彩一些!