人生三大境界
衣带渐宽终不悔,为伊消得人憔悴。
昨夜西风凋碧树,独上高楼,望尽天涯路。
众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

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__

posted @ 2021-10-18 16:42  一剑霜寒十九洲  阅读(1360)  评论(0编辑  收藏  举报