Set, copy in depth, copy in Depth

Source: Internet
Author: User

Set, copy in depth, copy in Depth

1. set: Aggregates different elements to form a set. It is also a data type in Python.

Unchangeable data type: Number, string, and tuples variable type: List, Dictionary

Purpose: remove duplicates from the list.

Test the link

1.1 create a set

1 s1 = set ("sang") variable set can be used to add or delete elements and cannot be used as Dictionary keys.
Dicic = {s1: "123"} unhashable type: 'set'
2 s2 = frozenset ('Alex ') unchangeable set 3 print (s2, type (s2 ))
Result:
{'s', 'a', 'g', 'n'} <class 'set'>frozenset({'a', 'l', 'x', 'e'}) <class 'frozenset'>
1 a = set([ 1,2,3,4,5 ])2 b = set([ 4,5,6,7,8 ])
3 c = set([2,2,3,5])
4 print(a)
5 print(b)
6 print(type(a))
7 print(type(b))
8 print(c)
Result:

{1, 2, 3, 4, 5}
{8, 4, 5, 6, 7}
<Class 'set'>
<Class 'set'>

{2, 3, 5}

As shown above, the final result shows the set type, and through the above (c), we can find that the elements of the set are not repeated, that is, they can be automatically de-duplicated, and the set is unordered (B)

1 li = [[1, 2], 'A', 'B'] 2 s = set (li) # TypeError: unhashable type: 'LIST' 3 print (s)
A collection object is a group of unordered hash values. Its members can be used as Dictionary keys.

1.2 access set

1 s = set ("sang") 2 print ("a" in s) 3 # s [1] # 'set' object does not support indexing
4 for I in s:
5 print (I)
Result:
1 1 True2 2 n3 3 s4 4 a5 5 g

1.3 Set Operations

Add

1 s = set("sang")2 s.add(2)3 s.add("alex")4 print(s)

Result:

1 {'s', 2, 'g', 'a', 'n', 'alex'}

Update adds Multiple Elements

1 s.update("SANG")2 print(s)

Result:

1 {2, 'a', 'g', 'n', 'A', 'G', 'alex', 's', 'S', 'N'}

Remove

1 s.remove("S")2 print(s)

Result:

1 {2, 'alex', 'g', 'A', 'a', 'N', 'n', 's', 'G'}

1.4 operator operations on a set

Intersection () intersection # union a = t | s t and s union difference set # parent set superset
1 a = set ([, 5]) 2 B = set ([, 8]) 3 # intersection () intersection 4 print (. intersection (B) 5 print (a & B) 6 7 # union a = t | the union of s t and s 8 print (. union (B) 9 print (a | B) 10 11 # difference set 12 print (. difference (B) # a-B in a not in b13 print (B. difference (a) # B-a in B not in a14 print (a-B) 15 print (B-a) 16 print (. symmetric_difference (B) # symmetric Ric symmetry, difference set, reverse intersection 17 print (a ^ B) 18 19 # parent set superset 20 print (. (B) # a> b21 print (. issubset (B) # subset issuperseta <B
Result:
1 F: \ Python installation \ python.exe "G:/Pycharm file/week4/day13 9.5/set. py "2 {4, 5} 3 {4, 5} 4 {1, 2, 3, 4, 5, 6, 7, 8} 5 {1, 2, 3, 4, 5, 6, 7, 8} 6 {1, 2, 3} 7 {8, 6, 7} 8 {1, 2, 3} 9 {8, 6, 7} 10 {1, 2, 3, 6, 7, 8} 11 {1, 2, 3, 6, 7, 8} 12 False13 False14 15 15 Process finished with exit code 0
 

2. Copy in Depth

Sometimes we may need to copy a list, but if we do this for many times, we need to open up a lot of space, obviously, this is not the case. So here we use the copy method, that is, copy,

Unchangeable data type: Number, string, and tuples variable type: List, Dictionary

Let's just give an example. Let's take a look at the differences in the results.

1 import copy 2 husband = ["xiaohu", 123, [,] 3 wife = husband. copy () 4 wife [0] = "xiaopang" 5 wife [1] = 456 6 wife [2] [1]-= 500 # Light 7 print (wife) 8 print (husband) 9 10 xiaosan = copy. deepcopy (husband) 11 xiaosan [0] = "jinxin" 12 xiaosan [1] = "789" 13 xiaosan [2] [1]-= 180014 print (xiaosan) # Deep 15 print (husband)
Result:
1 F: \ Python installation \ python.exe "G:/Pycharm file/week4/day13 9.5/deep copy. py "2 ['xiaopang ', 456, [15000,850 0] 3 ['xiaohu', 123, [15000,850 0] 4 ['jinxin', '123 ', [15000,670 0] 5 ['xiaohu ', 123, [15000,850 0] 6 7 Process finished with exit code 0

 

 

 

 

 

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.