Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the collection in Python

Shulou Source: shulou.com Published: 2022-06-01 06:48:31 09月10日 Update

This article mainly shows you "what is the collection in Python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the collection in Python" this article.

I. what is a set?

2. How to create a collection

Elements in the collection cannot be repeated

# location: Wuhan, Hubei # name: Pi Pi Zhi of Science and Engineering # Development time: 2022-2-27 19Rom 58pm 'first creation method' s = {1, 2, 4, 4, 4, 5, 4, 4, 5, 5, 7, print (s)''the second creation method Use set ()''s1=set (range (6)) print (S1) s2=set ([1y2) s3=set ((1) s4=set (' python') print (S4) s5=set ({7) 8pp99) print (s5) 'define an empty set' 's6=set () print (S6) III.

S = {10 in (10 in s) print (100 in s) print (10 not in s) print (100 not in s) 3.2 New operations of collection elements

Use add to add one, and update to add multiple

# location: Wuhan, Hubei # name: Pi Pi Zhi of the course of study and Engineering # Development time: 2022-2-28 22 print 42s = {10Magre 20je 30mins 50pyrus 50pyrus 70pime 90} s.add (100s) print (s) s.update ({200}) print (s) s.update ([111222333]) s.update ((44Magi 555066)) print (s) 3.3 deletion of collection elements

# location: Wuhan, Hubei # name: Pi Pi Zhi (Engineering) # Development time: 2022-2-28 22 s.discard 52s = {10 s.clear () print (s) 4, the relationship between sets

# location: Wuhan, Hubei # name: Pi Pi Zhi of the Science and Engineering Department # Development time: 2022-3-3 20 print 18 Pippi Zhi # Development time: 2022-3-3 20 print 20 print 30 40 40} s 2 = {30 print (20) print (s 1)''whether one set is a subset of another set''s = {10 line 20, 30 10, 50, 60, 80, 90} s 1 = {10, 10, 20, 30, 30, 40, 10, 10, 20, 30, 40, 10, 10, 20, 90, print (s1.issubset (s)) print (s2.issubset (S1)) 'one' Whether a set is a superset of another set''print (s.issuperset (S1)) print (s1.issuperset (S2))' 'whether there is no intersection between two sets' 'print (s1.isdisjoint (S2)) 5. Mathematical operation of set

# location: Wuhan, Hubei # name: Pi Pi Zhi of Science and Engineering # Development time: 2022-3-3 20 print 4pm intersection S1 = {10 s2print 20 30 40} S2 = {20 s1.union (S2)) # equivalent to S1 & S2 union print (s1.union (S2)) # equivalent to S1 | sonic difference set print (s1.difference (S2)) # equivalent to S1-s2print (s2.difference (S1)) # equivalent to S1-S1 symmetric difference set (s1.symmetric_difference (S2)) # is equivalent to S1 ^ s26, Set generation

# location: Wuhan, Hubei # name: Pippizhi of academic Engineering # Development time: 2022-3-3 21 print 21 for i in range (6)] print [I * for i in range (6)] print (lst)''set Generation''s = {ionomi for i in range (6)} print (s) Summary

The above is all the content of this article "what are collections in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Place name Engineering time Pippi Wuhan Hubei Development elements content methods articles Generation two intersection difference Learning help Symmetry between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Shulou Tech Info Huawei Apple