10 Python data type-set, python Data Type

Source: Internet
Author: User

10 Python data type-set, python Data Type

InPython setIs a basic data type.SetType, which can be set () or frozenset. CreateSet,Add a set,Delete collection,Intersection,Union,Difference setOperations are very practical.

  1. Similar to other languages, python set is a disordered, non-repeating element set. Its basic functions include link testing and deduplication. the Set object also supports mathematical operations such as union, intersection, difference, and sysmmetric difference.
  2. Sets support x in set, len (set), and for x in set. As an unordered set, sets do not record element positions or insertion points. Therefore, sets do not support indexing, slicing, or sequence-like operations.
1 x = set('spam')2 y = set(['h','a','m'])3 z = {1, 4, 5, 3, 7, 8}4 print(x, y, z)5 6 {'a', 'm', 's', 'p'} {'a', 'h', 'm'} {1, 3, 4, 5, 7, 8}
Set
1 x = set ('spam') 2 y = set (['h', 'A', 'M']) 3 z = {1, 4, 5, 3, 7, 8} 4 print (x, y, z) 5 print (x & y) # intersection 6 7 {'M', 'A'} 8 print (x | y) # Union set 9 10 {'P', 's', 'M', 'h', 'A'} 11 print (x-y) 12 print (y-x) # difference set 13 14 {'P', 's'} 15 {'H '}
Intersection, union, and difference

The set is unordered and does not repeat the data set. Its elements can be hashed (unchangeable type), but the set itself cannot be hashed (so the set cannot be used as a dictionary key). The following are the two most important points of the Set:

Deduplication: automatically removes duplicates when a list is changed to a set.

Test the relationship between the intersection, difference set, and union of the two groups of data.

1. Create a set

1 set1 = set({1,2,'barry'})2 set2 = {1,2,'barry'}3 print(set1,set2)  # {1, 2, 'barry'} {1, 2, 'barry'}
View Code

2. Add a set

1 set1 = {'Alex ', 'wusir', 'ritianc', 'egon', 'barry'} 2 set1.add ('goddess of view') 3 print (set1) 4 5 # update: 6 set1.update ('A') 7 print (set1) 8 set1.update ('instructor ') 9 print (set1) 10 set1.update ([1, 2, 2, 3]) 11 print (set1)
View Code

3. delete a set

1 set1 = {'Alex ', 'wusir', 'ritianc', 'egon', 'barry'} 2 3 set1.remove ('Alex ') # delete an element 4 print (set1) 5 6 set1.pop () # randomly delete an element 7 print (set1) 8 9 set1.clear () # Clear the set 10 print (set1) 11 12 del set1 # Delete set 13 print (set1)
View Code

4. Other operations on the set:

4.1 intersection. (& Or intersection)

1 set1 = {1,2,3,4,5}2 set2 = {4,5,6,7,8}3 print(set1 & set2)  # {4, 5}4 print(set1.intersection(set2))  # {4, 5}
View Code

4.2 union. (| Or union)

1 set1 = {1,2,3,4,5}2 set2 = {4,5,6,7,8}3 print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7}4 5 print(set2.union(set1))  # {1, 2, 3, 4, 5, 6, 7}
View Code

4.3 difference set. (-Or difference)

1 set1 = {1,2,3,4,5}2 set2 = {4,5,6,7,8}3 print(set1 - set2)  # {1, 2, 3}4 print(set1.difference(set2))  # {1, 2, 3}
View Code

4.4. (^ Or symmetric_difference)

1 set1 = {1,2,3,4,5}2 set2 = {4,5,6,7,8}3 print(set1 ^ set2)  # {1, 2, 3, 6, 7, 8}4 print(set1.symmetric_difference(set2))  # {1, 2, 3, 6, 7, 8}
View Code

4.5 subset and superset

1 set1 = {1, 2, 3} 2 set2 = {1, 2, 3, 4, 5} 3 4 print (set1 <set2) 5 print (set1.issubset (set2) # these two are the same, both indicate that set1 is a subset of set2. 6 7 print (set2> set1) 8 print (set2.issuperset (set1) # The two are the same, indicating that set2 is the set1 superset.
View Code

5. The frozenset variable set changes the set to an unchangeable type.

1 s = frozenset('barry')2 print(s,type(s))  # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>
View Code 2, in-depth copy

1. first look at the value assignment operation.

 1 l1 = [1,2,3,['barry','alex']] 2 l2 = l1 3  4 l1[0] = 111 5 print(l1)  # [111, 2, 3, ['barry', 'alex']] 6 print(l2)  # [111, 2, 3, ['barry', 'alex']] 7  8 l1[3][0] = 'wusir' 9 print(l1)  # [111, 2, 3, ['wusir', 'alex']]10 print(l2)  # [111, 2, 3, ['wusir', 'alex']]
View Code

For the value assignment operation, l1 and l2 point to the same memory address, so they are exactly the same.

2. copy a copy.

 1 l1 = [1,2,3,['barry','alex']] 2  3 l2 = l1.copy() 4 print(l1,id(l1))  # [1, 2, 3, ['barry', 'alex']] 2380296895816 5 print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2380296895048 6  7 l1[1] = 222 8 print(l1,id(l1))  # [1, 222, 3, ['barry', 'alex']] 2593038941128 9 print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 259303894189610 11  12 l1[3][0] = 'wusir'13 print(l1,id(l1[3]))  # [1, 2, 3, ['wusir', 'alex']] 173231565901614 print(l2,id(l2[3]))  # [1, 2, 3, ['wusir', 'alex']] 1732315659016
View Code

For light copy, the first layer creates a new memory address, and from the second layer, it points to the same memory address. Therefore, for the second and deeper layers, maintain consistency.

3. Copy deepcopy in depth.

 1 import copy 2 l1 = [1,2,3,['barry','alex']] 3 l2 = copy.deepcopy(l1) 4  5 print(l1,id(l1))  # [1, 2, 3, ['barry', 'alex']] 2915377167816 6 print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 2915377167048 7  8 l1[1] = 222 9 print(l1,id(l1))  # [1, 222, 3, ['barry', 'alex']] 291537716781610 print(l2,id(l2))  # [1, 2, 3, ['barry', 'alex']] 291537716704811 12 l1[3][0] = 'wusir'13 print(l1,id(l1[3]))  # [1, 222, 3, ['wusir', 'alex']] 291537716724014 print(l2,id(l2[3]))  # [1, 2, 3, ['barry', 'alex']] 2915377167304
View Code

For deep copy, the two are completely independent, changing any element of any one (no matter how many layers), and the other is never changed.

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.