Example of how python implements merging multiple lists and merging multiple django QuerySets

  • 2021-06-28 12:56:09
  • OfStack

This article provides an example of how python implements merging multiple lists and merging multiple django QuerySets.Share it for your reference, as follows:

When writing a few gadget applications with python or django, you may encounter situations where multiple lists are combined to one list.From a technical point of view, it is not difficult to handle and there are many ways to think about it, but I think there is a very simple and efficient way that I haven't noticed before.That is to use the chain method to merge multiple lists. It can also be used to merge QuerySet of django.

1. python uses chain to merge multiple lists

chain is implemented with C, which is reliable in natural performance.Here's the basic usage:


#coding:utf-8
from itertools import chain
a = [1,2,"aaa",{"name":"roy","age":100}]
b = [3,4]
c = [5,6]
#items = a + b + c
items = chain(a,b,c)
for item in items:
  print item

The output is as follows:

1
2
aaa
{'age': 100, 'name': 'roy'}
3
4
5
6

This shows a very good merge success.

2. Consolidate multiple QuerySets with chain in Django.

It is possible to merge multiple QuerySets of the same model in Django itself.


#coding:utf-8
from itertools import chain
from yihaomen.common.models import Article
articles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('autoid','title')
articles2 = Article.objects.filter(autoid = 30).values('autoid','title')
articles = articles1 | articles2 #  Note how this is done.If  Model Same, without slicing, and field 1 This can then be used 
print articles1
print articles2
print articles

This works well, but with some limitations, it's sufficient in many cases for Django to merge into one QuerySet and return to the template engine for processing.

Of course, it can also be implemented with chain, which is more convenient and less restrictive. Even the data queried from different MODELs can be easily merged into one list.


#coding:utf-8
from itertools import chain
from yihaomen.common.models import Article, UserID
articles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('autoid','title')
users = UserID.objects.all()
items = chain(articles1, users)
for item in items:
  print item

This is both more convenient and practical, and it is convenient to handle certain lists that need to be merged and then transferred to a certain location.

More readers interested in Python-related content can view this site's topics: Python List (list) Operation Skills Summary, Python Encoding Operation Skills Summary, Python Data Structure and Algorithms Tutorial, Python Function Usage Skills Summary, Python String Operation Skills Summary,Introduction to Python and Advanced Classic Tutorials and Summary of Python File and Directory Operating Skills

I hope that the description in this paper will be helpful to everyone's Python program design.


Related articles: