

python tip : zip-hqtmit的博客
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)))
结果
补充
######################
########################
另一个例子
#/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)
Recommend
-
50
classClasses(models.Model):"""班级表"""title=models.CharField(max_length=32)teachers=models.ManyToManyField("Teacher")生成表c1=Classes.objects.filter(id=1).first()#获取1个对象c1.teachers.add(1)#添加1个teacher对象,tea
-
45
idea的outline功能在pycharm中如何展示删除汉化点击strcture就能显示了。
-
5
【Python】zip 2018年09月27日 Author: Guofei 文章归类: Python语法 ,文章编号: 1241 版权声明:本文作者是郭飞。转载随意,但需要标明原文链...
-
11
Python's zipapp: Build Executable Zip Applications
-
11
Append a file into a zip file in Python I was trying to find a way to append a file into a zip file in python,...
-
8
Python Zip Imports: Distribute Modules and...
-
14
Manipulate Your ZIP Files Efficiently – Real PythonGetting Started With ZIP Files ZIP files are a well-known and popular tool in today’s digital world. These files are fairly popular and widely used for cross-platfor...
-
5
Writing JSON into a Zip file with PythonAugust 20, 2020 · 4 min · Benjamin BengfortFor scientific reproducibility, it has become common for me to output experimental results as zip files that contain both configuratio...
-
3
April 13, 2022 /
-
8
Manipulating ZIP Files With Python Python’s zipfile is a standard-library module intended to manipulate ZIP fil...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK