The database filters the user, then strips off a portion (the list is poor) and then randomly returns a user. SQLAlchemy + Python Collection (set) + random

Source: Internet
Author: User

The things between SQLAlchemy and Flask-sqlalchemy are not too clear.

SQLAlchemy documentation too damn much, day.


The instance that was encountered today.


The user randomly matches, and the system randomly returns a user under certain filter conditions. For the user experience, you have to get rid of the people you already care about.


First get a list of users that meet the filter criteria:

Users = User.query.filter_by (School=form.school.data, Sex=form.sex.data, Status=form.status.data). All ()

And to get a list of people you've followed:

followed = G.user.followed.all ()  


The point is, how do you subtract the other list from the first one? Without this method, the time complexity should be super large with a for loop and if judgment.

Therefore, you should first list to set and then set to find the difference set and then go to the list.

Filter_users = List (set (users)-set (followed))    # Lists to set to find the difference set and then to go to the list

Then we are going to start randomly selecting a user. Find the documentation for the Python random module and find that choice () is a good, random selection in the sequence.

Note that if it is an empty sequence, it will be an error.

Therefore, the following code is:

            If filter_users:                user = Random.choice (filter_users)                Flash (U ' you match to the following users: ')                return redirect (' Profile ', id=user.id)]            else:                Flash (U ' ah oh, no user satisfies this condition ')    # later use the mascot to express, do not use Flash                return render_template ( ' match.html ', form=form)



Above.

Today learned a lot of things, what hash, data structure, time complexity are out.

Cock.

The database filters the user, then removes a portion (the list is poor) and then randomly returns a user. SQLAlchemy + Python Collection (set) + random

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.