Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use collection Set in Python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to use the collection Set in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In mathematics, a strict definition of a set can be abstract and difficult to grasp. But in fact, you can think of a collection simply as a collection of well-defined different objects, often referred to as elements or members.

Python provides a built-in collection type to group objects into a collection. Collections differ from other object types in the unique operations that can be performed on.

Define a collection

The collection is unordered, and the elements are unique, and the collection itself can be modified, but the elements contained in the collection must be immutable.

The way collections are built

# the constructed set data will be deduplicated automatically x = set () # list > x = set (['foo',' bar', 'baz',' foo', 'qux']) > x {' qux', 'foo',' bar', 'baz'} # tuple > x = set ((' foo', 'bar',' baz', 'foo',' qux')) > > x {'qux',' foo', 'bar' 'baz'} # string mode > s =' quux' > list (s) ['QQ, 'UFG,' UBG,'x'] > > set (s) {'XY, 'UFG,' q'}

Collection elements are automatically sorted after set and elements must be immutable.

> > x = {42, 'foo', (1, 2, 3), 3.14159} > x {42,' foo', 3.14159, (1, 2, 3)} # list and dict cannot be set > a = [1,2,3] > {a} Traceback (most recent call last): File ", line 1, in {a} TypeError: unhashable type: 'list' > d = {' a}: 1 {d} Traceback (most recent call last): File ", line 1, in {d} TypeError: unhashable type: 'dict' collection size and membership

Methods the application of len (), in and not in.

> x = {'foo',' bar', 'baz'} > len (x) 3 >' bar' in xTrue > 9 operations of the qux' in xFalse collection

Calculate the union of sets

# x1.union (x2 [, x3...]) # x1 | x2 [| x3...] > x1 = {'foo',' bar', 'baz'} > x2 = {' baz', 'qux',' quux'} > > x1 | x2 {'baz',' quux', 'qux',' bar', 'foo'} > > x1.union (x2) {' baz', 'quux',' qux', 'bar' 'foo'} # more collection union operations > a = {1, 2, 3, 4} > b = {2, 3, 4, 5} > > c = {3, 4, 5, 6} > d = {4, 5, 6, 7} > > a.union (b, c, d) {1, 2, 3, 4, 5, 6, 7} > > a | b | d {1, 2, 3, 4, 5, 6, 7} to calculate the set intersection

# x1.intersection (x2 [, x3...]) # x1 & x2 [& x3...] > > x1 = {'foo',' bar', 'baz'} > > x2 = {' baz', 'qux',' quux'} > x1.intersection (x2) {'baz'} > x1 & x2 {' baz'} # more set intersection operations > > a = {1,2,3,4} > b = {2,3,4,5} > c = {3,4,5 6} > d = {4,5,6,7} > a.intersection (b, c, d) {4} > a & b & c & d {4} calculate the difference between sets

# x1.difference (x2 [, x3...]) # x1-x2 [- x3...] > > x1 = {'foo',' bar', 'baz'} > x2 = {' baz', 'qux',' quux'} > x1.difference (x2) {'foo',' bar'} > > x1-x2 {'foo',' bar'} # more set differential operations > > a = {1,2,3,30,300} > b = {10,20,30 40} > c = {100,200,300,400} > a.difference (b, c) {1,2,3} > > a-b-c {1,2,3} calculates the symmetry difference between sets

# x1.symmetric_difference (x2) # x1 ^ x2 [^ x3...] > > x1 = {'foo',' bar', 'baz'} > x2 = {' baz', 'qux',' quux'} > x1.symmetric_difference (x2) {'foo',' qux', 'quux',' bar'} > x1 ^ x2 {'foo',' qux', 'quux',' bar'} # more set symmetric difference operations > a = {1,2,3 4, 5} > b = {10,2,3,4,50} > > c = {1,50,100} > > a ^ b ^ c {100,5,10} calculated whether there are elements # x1.isdisjoint (x2) > x1 = {'foo',' bar', 'baz'} > x2 = {' baz', 'qux',' quux'} > x1.isdisjoint (x2) False > > x2-{'baz'} {' quux'' 'qux'} > x1.isdisjoint (x2-{' baz'}) True# x1.isdisjoint (x2) is True Then x1 & x2 is an empty set > x1 = {1,3,5} > x2 = {2,4,6} > x1.isdisjoint (x2) True > x1 & x2set () calculates whether a collection is a subset of another set # x1.issubset (x2) # x1 > > x1 = {'foo',' bar', 'baz'} > x1.issubset ({' foo', 'bar',' baz', 'qux',' quux'}) True > x2 = {'baz',' qux' 'quux'} > > x1 > > x = {1,2,3,4,5} > x.issubset (x) True > > x > > x1 = {' foo', 'bar'} > x2 = {' foo', 'bar',' baz'} > > x1

< x2True>

> > x1 = {'foo',' bar', 'baz'} > x2 = {' foo', 'bar',' baz'} > x1

< x2False# 子集与真子集的判断>

> > x = {1,2,3,4,5} > x > > x

< xFalse计算一个集合是否是另一个集合的超集# x1.issuperset(x2)# x1 >

= x2 > x1 = {'foo',' bar', 'baz'} > x1.issuperset ({' foo', 'bar'}) True > x2 = {' baz', 'qux',' quux'} > > x1 > = the x2False# collection is considered a subset of itself Default is self-superset > > x = {1,2,3,4,5} > x.issuperset (x) True > > x > = xTrue to calculate whether a collection is the correct superset of another collection # x1 > x2 > > x1 = {'foo',' bar', 'baz'} > x2 = {' foo', 'bar'} > > x1 > x2True > > x1 = {' foo', 'bar',' baz'} > > x2 = {'foo',' bar' 'superset 'baz'} > x1 > x2False# collection is not its own correct superset > x = {1, 2, 3, 4, 5} > x > 9 modifications of xFalse collection

Although the elements contained in the collection must be immutable, the collection itself can be modified.

Update computing union # x1.update (x2 [, x3...]) # x1 | = x2 [| x3...] > x1 = {'foo',' bar', 'baz'} > x2 = {' foo', 'baz',' qux'} > > x1 | = x2 > x1 {'qux',' foo', 'bar',' baz'} > x1.update (['corge',' garply']) > > x1 {'qux',' corge', 'garply',' foo' 'bar',' baz'} intersection_update calculation intersection # x1.intersection_update (x2 [, x3...]) # x1 & = x2 [& x3...] > > x1 = {'foo',' bar', 'baz'} > x2 = {' foo', 'baz',' qux'} > > x1 & = x2 > > x1 {'foo',' baz'} > > x1.intersection_update (['baz'') 'qux']) > x1 {' baz'} difference_update modifies processed collections according to differences > x1 = {'foo',' bar', 'baz'} > x2 = {' foo', 'baz',' qux'} > x1-= x2 > > x1 {'bar'} > x1.difference_update ([' foo', 'bar'') 'qux']) > x1set () symmetric_difference_update modifies the processed set # x1.symmetric_difference_update (x2) # x1 ^ = x2 > > x1 = {' foo', 'bar',' baz'} > > x2 = {'foo',' baz', 'qux'} > > x1 ^ = x2 > > x1 {' bar', 'qux'} > x1.symmetric_difference_update ([' qux', 'corge']) > > x1 {' bar'] The 'corge'} add element is added to the collection > x = {' foo', 'bar',' baz'} > x.add ('qux') > x {' bar', 'baz',' foo', 'qux'} remove collection to remove an element > x = {' foo', 'bar',' baz'} > x.remove ('baz') > x {' bar' Throw an exception if the element step exists > x.remove ('qux') Traceback (most recent call last): File ", line 1, in x.remove (' qux') KeyError: remove an element from the qux'discard collection > x = {'foo',' bar', 'baz'} > x.discard (' baz') > > x {'bar',' foo'} > > x.discard ('qux') > > x {' bar' Remove a random element from the 'foo'} pop collection > > x = {' foo', 'bar',' baz'} > x.pop () 'bar' > x {' baz', 'foo'} > > x.pop ()' baz' > > x {'foo'} > > x.pop ()' foo' > > xset () > x.pop () Traceback (most recent call last): File ", line 1 In x.pop () KeyError: 'pop from an empty set'clear empties the collection > x = {' foo', 'bar',' baz'} > x {'foo',' bar', 'baz'} > x.clear () > xset () is frozen

Freezeset is the built-in type of Python and is immutable and inoperable.

> x = frozenset (['foo',' bar', 'baz']) > xfrozenset ({' foo', 'baz',' bar'}) > len (x) 3 > > x & {'baz',' qux', 'quux'} frozenset ({' baz'})

An attempt to modify the freezeset will fail

> > x = frozenset (['foo',' bar', 'baz']) > x.add (' qux') Traceback (most recent call last): File ", line 1, in x.add ('qux') AttributeError:' frozenset' object has no attribute 'add' > x.pop () Traceback (most recent call last): File", line 1, in x.pop () AttributeError:' frozenset' object has no attribute 'pop' > > x.clear () Traceback (most recent call last): File "" Line 1, in x.clear () AttributeError: 'frozenset' object has no attribute' clear' > xfrozenset ({'foo',' bar', 'baz'}) Thank you for reading! This is the end of the article on "how to use Set in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report