30

python tip : zip-hqtmit的博客

 6 years ago
source link: http://blog.51cto.com/13606158/2073520
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python tip : zip

目标:
由python orm  model.objects.all().values("id")
获取到queryset 格式 [(1,),(2,),(3,)]    
转换为格式 (1,2,3) ,以便在jinja2模版中调用
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# info: 

list1=[5,5,6]
list2=[1,2,3]
zipresult=zip(list1,list2)
print("1. zipresult: %s"%zipresult)

listresult=list(zip(list1,list2))
print("2. listreult: %s"%listresult)

### zip(*list)方式抽取,抽取后需要定位【0】
orilist_from_listresult=list(zip(*listresult))
print("3. orilist_from_listresult: %s"%orilist_from_listresult)

ori_list1=orilist_from_listresult[0]
ori_list2=orilist_from_listresult[1]
print("4. ori_list1: %s "%(ori_list1,))
print("5. ori_list2: %s 类型:%s"%(ori_list2,type(ori_list2)))

结果
python tip : zip

补充
python tip : zip

######################
########################
另一个例子

#/usr/bin/env python3

a = [
    ['u1', 'u2', 'u3'],  # username
    ['p1', 'p2', 'p3'],  # password
    [0, 2, 3],  # count 已错误次数
]

b=list(zip(*a))
c=list(zip(*b))

print(b)
print(c)

python tip : zip


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK