Addition, deletion, modification and inspection of Python learning set (set)

set – a close relative of the dictionary

The two major functions of the collection:

  • Collections can implement the function of deduplication
  • Sets can implement relationship testing; intersection, difference, union, whether subset, whether there is no intersection

Definition of a set

  • Collections are non-repeating data structures
  • If the curly braces are empty, it is a dictionary type
s = {}
 print  type (s)
#<type 'dict'>
  • 1
  • 2
  • 3
  • The first way to define a collection
s = { 1 , 2 , 3 , 4 }
 print type (s) 
#<type 'set'>
  • 1
  • 2
  • 3
  • The second way to define a collection
s = set()
print type(s)
#<type 'set'>

s = set([1,2,3,1,2,3])
print s
#set([1, 2, 3])

s = set("hello")
print s
#set(['h', 'e', 'l', 'o'])

s = set((1,2,3,4,5))
print s
#set([1, 2, 3, 4, 5])

s = set({'a':1, 'b':2, 'c':3})
#set(['a', 'c', 'b'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Collection properties

  • Collections are unordered, non-repeating data types;
  • Therefore does not support indexing, nor does it support slicing; nor does it support repetition; nor does it support joins;
  • support member operator;
  • Support for loops;
#Support member operator 
s = { 1 , 2 , 3 , 4 , 1 , 2 }
 print 1 in s
 #True print 2 not in s
 #False  
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#The collection supports for loop and is iterable 
s = { 1 , 2 , 3 , 2 , 1 , 5 , 6 }
 for i in s :
     print i ,
 #1 2 3 5 6
  • 1
  • 2
  • 3
  • 4
  • 5

CRUD

Increase

s = {1,2,3,4,1,2,3}
print s
#set([1, 2, 3, 4])

s.add(1) 
#set([1, 2, 3, 4])

s.add(7)
print s
#set([1, 2, 3, 4, 7])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
s = {1,2,3,2,1,5,6}
s1 = {'a', 'b', 'c'}
s.update(s1)
print s
#set(['a', 1, 2, 3, 5, 6, 'c', 'b'])
  • 1
  • 2
  • 3
  • 4
  • 5

Find (Relational Test Operation)

  • mathematical relationship
s1 = { 1 , 2 , 3 , 4 }
s2 = { 1 , 2 , 3 , 5 }

# intersection
print s1 & s2
#set([1, 2, 3])

# union
print s1 | s2
#set([1, 2, 3, 4, 5])

# Complement
print s1 - s2
#set([4])
print s2 - s1
#set([5])

# peer-to-peer difference
print s1 ^ s2
#set([4, 5])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • collection method
s1 = { 1 , 2 , 3 , 4 }
s2 = { 1 , 2 , 3 , 5 }

# intersection
print s1.intersection(s2)
#set([1, 2, 3])


# union
print s1.union(s2)
#set([1, 2, 3, 5])

# Complement
print s1.difference(s2)
#set([4])
print s2.difference(s1)
#set([5])

# peer-to-peer difference
print s1.symmetric_difference(s2)
#set([4, 5])

#supplement update 
s1 = { 1 , 2 , 3 , 4 }
s2 = { 1 , 2 , 3 , 5 }
print s1.intersection_update(s2)
#set([1, 2, 3])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

delete

  • pop( ) deletes randomly
s = {'a','hello', 'redhat', 'b', 18, 33, 4, 1, 2, 7, 6, 5}
print s
#set(['a', 1, 2, 'b', 4, 5, 6, 7, 18, 'redhat', 33, 'hello'])
print s.pop()
#a
  • 1
  • 2
  • 3
  • 4
  • 5
  • remove( ) deletes the specified element of the set, if it does not exist, an error is reported
s = {'alice','hello', 'westos', 'b', 18}
s.remove('alice')
print s
#set([18, 'b', 'westos', 'hello'])
  • 1
  • 2
  • 3
  • 4
  • discard( ) deletes the specified element of the set, if it does not exist, do nothing
s = {'alice','hello', 'westos', 'b', 18}
s.discard("a")
print s
#set([18, 'b', 'westos', 'alice', 'hello'])
  • 1
  • 2
  • 3
  • 4
  • clear( ) clears the collection element
s = {'alice','hello', 'westos', 'b', 18}
s.clear()
print s
#set([])
  • 1
  • 2
  • 3
  • 4

application:

  • (Huawei computer test questions) Title description
    Mingming wanted to invite some students to do a questionnaire survey in school. For the objectivity of the experiment, he first used a computer to generate N random integers between 1 and 1000 (N≤1000), N is input by the user. For the repeated numbers, only one is reserved, and the rest of the same numbers are removed. Different numbers correspond to different student IDs. Then sort these numbers from small to large, and go to the classmates to do
    the survey . Please help Mingming to complete the work of "de-duplication" and "sorting";
  • hint:
#Generate random numbers 
import random 
random .randint( 1 , 1000 )
 #Deduplication of lists
  • 1
  • 2
  • 3
  • 4
  • code show as below:
import random
s = set()
N = input( 'Enter a number:' )
 for i in range (N) :
    s.add(random.randint(1,1000))

# sorted is a built-in method for sorting;
print sorted(s)

#sort by list 
# li = list(s) 
# li.sort() 
# print li
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Summarize

  • Mutable data types: lists, dictionaries, sets
  • Immutable data types: numeric types, strings, tuples
    • A variable data type implements a function and directly changes the variable data type;
    • An immutable data type implements a function and needs to assign the result to another variable;
  • Whether to implement a for loop
    • Iterable data types: str, list, tuple, dict, set
    • Non-iterable data types: numeric types
  • Whether to support index, slice, repeat and join features
    • ordered data types: str, list, tuple
    • Unordered data types: dict, set

Tags: Addition, deletion, modification and inspection of Python learning set (set)

Python Python gather set

Related: Addition, deletion, modification and inspection of Python learning set (set)