Python full stack development-Day5 collection, python-day5 collection
Full-stack python development-Day5 collection1. First, start learning the set according to the following points:#1. Basic usage 1. Purpose 2. Definition method 3. Common Operations + Built-in methods #2: summary 1. Save one value or store multiple values. Only one value can store multiple values, what type can all values be? 2. ordered or unordered. 3. variable or unchangeable !!! Variable: The value changes, and the id remains unchanged. Variable = non-hash !!! Immutable: The value changes, and the id changes. Immutable = hash II. Set # function: deduplication, relational operations, # Review of knowledge point variable type is non-hash type immutable type is hash type # define set: Set: multiple elements can be contained and separated by commas. The elements in the Set follow three principles: 1. Each element must be of an unchangeable type (hash can be used as the dictionary key). 2: element 3 with no duplicates: unordered attention sets are stored together to store different values. Different sets are used for relational operations, no need to tangle with a single value in the set # preferred operations: #1. Length len #2. member operations in and not in #3. & Intersection
1 stus_linux = {'Alex ', 'egon', 'zhang quaneg', 'Li tie eg', 'oldboys'} 2 stus_python = {'Li Erya', 'wxx ', 'liudehua', 'Alex ', 'egon'} 3 4 # students register for both linux and python: intersection 5 print (stus_linux & stus_python) 6 print (stus_linux.intersection (stus_python )) 7 8 # All values are {'Alex ', 'egon '}#4. | collection
1 stus_linux = {'Alex ', 'egon', 'zhang quaneg', 'Li tie eg', 'oldboys'} 2 stus_python = {'Li Erya', 'wxx ', 'liudehua', 'Alex ', 'egon'} 3 4 # All students: Union set 5 print (stus_linux | stus_python) 6 print (stus_linux.union (stus_python )) 7 8 # values are 9 # {'oldboys', 'egon', 'liudehua', 'zhang quaneg', 'Li Erya ', 'wxx', 'Alex ', 'tie ieg '}#5.-difference set
1 stus_linux = {'Alex ', 'egon', 'zhang quaneg', 'Li tie eg', 'oldboys'} 2 stus_python = {'Li Erya', 'wxx ', 'liudehua', 'Alex ', 'egon'} 3 4 # register for linux only, but not for python: difference set 5 print (stus_linux-stus_python) 6 print (stus_linux.difference (stus_python) 7 8 # value: {'oldboy ', 'Li tie eg', 'zhang Quan eg'} 9 10 11 # register only python, not registered for linux: differential set 12 print (stus_python-stus_linux) 13 print (stus_python.difference (stus_linux) 14 15 # value: {'liudehua', 'Li Erya ', 'wxx '}#6. ^ symmetric difference set
1 stus_linux = {'Alex ', 'egon', 'zhang quaneg', 'Li tie eg', 'oldboys'} 2 stus_python = {'Li Erya', 'wxx ', 'liudehua', 'Alex ', 'egon'} 3 4 # names of students who have not registered for two courses at the same time: Crosstab set 5 print (stus_linux ^ stus_python) 6 print (stus_linux.w.ric_difference (stus_python) 7 8 9 # The values are {'Li tie eg', 'Li Erya ', 'oldboy', 'liudehua', and 'zhang quaneg ', 'wxx '}#7. View
1 s1 = {1, 'A', 'B', 'C', 'D'} 2 3 for item in s1: 4 print (item) 5 6 # values are sorted randomly by the preceding five elements.#8. Add
1 s1 = {'A', 'B', 'C'} 2 s1.add ('D') # Add a value 3 s1.add (4) 4 print (s1) at a time) 5 6 # The value is {'A', 'B', 'C', 'D'} 7 s1.update ({3, 4, 5 }) # add multiple values at a time: 8 print (s1) 9 10 # values: {'A', 'B', 'C', 'D', 3,4, 5} the order of the elements here is random.#9. Delete
1 s1 = {'A', 'B', 'C'} 2 # s1.discard () # When the deleted element does not exist, no error is reported. 3 s1.discard (4) 4 print (s1) # The value is {'A', 'B', 'C'} 5 6 # s1.remove () # When the deleted element does not exist, error 7 s1.remove (4) # error 8 9 s1.pop () # randomly remove an element 10 res = s1.pop () 11 print (res) # The value is random.#10. Parent set, subset:
1 # parent set: Dad contains son 2 # s1 = {1, 2, 3} 3 # s2 = {1, 2} 4 s1> s2 # represents s1 contains s2, s1 is s2 parent set 5 s2 <s1 # s2 is s1 subset