Python 3 Tutorial - Sets

Publié le: 10 juillet 2014
sur la chaîne: Gerald Senarclens de Grancy
3,432
40

In this video tutorial you will learn what Python sets are and what they can do for you.

The demonstrations include intersection, union and difference set operations - both using member functions and with operators. Finally a brief example of how sets can positively affect the performance of your programs is shown.

The following source code was used in this video tutorial:
s1 = {1, 2, 9, 3, 1, 3, 7, 9, 3, 1, 3, 2}
s2 = {5, 8, 4, 3, 7, 8}

intersection = s1.intersection(s2)
s1 & s2 # binary and operator

union = s1.union(s2)
s1 | s2 # binary or operator

s1.difference(s2)
s1 - s2

example of how sets can help improve your program's performance
by removing a nested for loop (O(n*m))
query1 = list(range(1, 100000, 2)) # 1, 3, 5, ...
query2 = list(range(1, 100000, 3)) # 1, 4, 7, ...

nested loop
records = []
for record in query1:
if record in query2:
records.append(record)

and now with sets
queryset1 = set(query1)
queryset2 = set(query2)
records = list(queryset1.intersection(queryset2))

--
This screencast was recorded using the kate editor, ipython3 shell and Kazam screencaster on Kubuntu 14.04.


Sur cette page du site, vous pouvez voir la vidéo en ligne Python 3 Tutorial - Sets durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Gerald Senarclens de Grancy 10 juillet 2014, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 3,432 fois et il a aimé 40 téléspectateurs. Bon visionnage!