python collection (set)

1, the set of set
  conventions:
    set translated into a set of
    collection is a collection of translation type, is a broad concept
  set:
    variable, disorder, non-repeating set of elements
  constructor:
    set () → Object set new new empty
    set (Iterable ) → new set object

# -*- coding:utf-8 -*-
# version:python3.7

s1 = set()    # empty set
s2 = {1,2,3}  # set
s3 = {}       # dict:字典,not set
s4 = set(range(5))
s5 = {'abc',1,1,2,2,3,3,'abc'}    # set:去重
print(s1,type(s1))
print(s2,type(s2))
print(s3,type(s3))
print(s4)
print(s5)

执行结果:
set() <class 'set'>
{1, 2, 3} <class 'set'>
{} <class 'dict'>
{0, 1, 2, 3, 4}
{1, 2, 3, 'abc'}

  Requirements set of elements must be hash
  type is not available hash of the current learned there list, set

# - * - Coding: UTF-. 8 - * - 
# Version: python3.7 

S1 = {1,2, ' ABC ' , (. 3,), [. 4 ]}
 Print (S1) 

Results of: error 
TypeError S1
= { 1,2, ' ABC ' , (. 3,), [. 4 ]} TypeError: unhashable type: ' List '

  Elements can not use the index
  set can be iterated

2, set of CRUD
  1> add (elem)
    add an element to this set
    if the element exists, do nothing

  2> update (* others)
    were combined to other elements to set collection
    parameters must iterables others
    place modification

  3> remove (elem)
    removes an element from the set of the
    element does not exist, an exception is thrown KeyError, Key: The only

  4> discard (elem)
    removes an element from the set of the
    element does not exist, do nothing

  5> pop () → item
    is removed and returned to any element
    empty set abnormal return KeyError

  6> clear ()
    Removes all elements

# - * - Coding: UTF-. 8 - * - 
# Version: python3.7 

S1 = {l, 2,3 }
 Print (S1) 
s1.add ( . 4)     # increase 
Print (S1) 
s1.update (Range ( . 6 ))     # combined with other elements to the collection, and to re- 
Print (s1) 
s1.remove ( 1) # remove 
Print (s1) 
s1.discard ( 11)          # removed, the element does not exist, do nothing 
Print ( S1) 
s1.pop ()      # eject 
Print (S1) 

execution result: 
{ 1, 2, 3 } 
{ 1, 2, 3, 4 }
{0, 1, 2, 3, 4, 5}
{0, 2, 3, 4, 5}
{0, 2, 3, 4, 5}
{2, 3, 4, 5}

  Modify
    or delete, or add new elements, without modifying
  the query
    nonlinear structure, not an index
  to traverse
    iterate through all the elements of
  members of the operators
    in and not in determining whether the elements in the set, the efficiency of it?

  Tested in IPython:

 

 

  After testing, the query time complexity of the linear structure is O (n), i.e., with an increase in the data size increases time-consuming, set, dict other structure, internal hash value as a key, the time complexity can be done 0 ( 1), the query time and data independent of scale

  Can hash
    value type int, float, complex
    Boolean True, False
    String String, bytes
    tuple
    None
  above are immutable type, a hash type is, Hashable

  elements are compulsory set of hash can be

3, the basic concepts set
  1 > collection
    collection of all the elements, for example, real numbers, the set of all real numbers is corpus

  2> subset superset subset and superset of
    a set A of all the other elements in the set B, A is a subset of B, B is superset of A

  3> true subset and superset
    A is a subset of B and A is not equal to B, A is a subset of B, B is superset of the set A

  4> and sets: a set of a plurality of combined results
    union (* others)
      returning a new collection and the plurality of sets of combined
    | operator overloading
      equivalents union

    update (* others)
      and a plurality of sets of combined in-place modification
    | =
      equivalents update

  5> intersection: a plurality of sets of common portions of
    intersection (* others)
      Returns the intersection, and a plurality of sets of
    &
      equivalent to the intersection

    intersection_update (* others)
      acquiring a plurality of sets of the union and intersection, and in situ modification
    & =
      equivalent intersection_update

  6> difference set: collection and removal of other portions of the set of common
    difference (* others)
      Returns a collection of a plurality of sets of the difference
    -
      equivalent to the difference

    difference_update (* others)
      acquisition and difference sets and a plurality of sets of modified spot
    - =
      equivalent difference_update

  7> symmetric difference
    of A and B, the intersection set of all elements that do not belong to A and B of the composition, referred to as (AB) ∪ (BA)
    symmetric_differece (OTHER)
      returns the difference between set and another set of
    ^
      equivalents symmetric_differece

    symmetric_differece_update (other)
      acquired difference set and another set and modified in situ
    ^ =
      equivalents symmetric_differece_update

  8> issubset (other), < =
      is determined whether the current collection is a collection of another set of sub-
    set1 <set2
      determines whether set1 set2 subset of
    issuperset (other),> =
      is determined whether the current set is a superset of other
    set1 > set2
      determined whether set1 set2 is true superset
    isdisjoint (other)
      current set and the other set is no intersection
    is no intersection, return true

. 4, set of applications:
  1> in common (intersection operator)
  your friend a, B, C, his friend C, B, D, seeking mutual friend

  2> micro-channel group to remind
  X and the group of other people are not micro-channel friendships!
    Union: userid in (A | B | C ...) == False, A, B, C and other micro-letter friends and set user ID is not in this and focus, shows that he is a friend and no one
    group where everyone else ids are not in X's friends list T & ids == set ()

  3> authority judging
  a the API, requesting permission along with A, B, C to access, user rights are B, C, D, determines whether the user can access the API
         the API set A, set user privileges P. To include all user rights permissions API requirements.
         AP = set (), AP is the empty set, comprising instructions P A
         A.issubset (P) is also OK, A is also a subset of rows P
         A & P = A line also

  There is a the API, requesting permission comprises A, B, any C one can access, user rights are B, C, D, judging whether the user can access the the API
         the API set A, set of permissions P
         A & P! = SET () can be
         A.isdisjoint (P) == False expressed intersection

  4> a general task list, all storage tasks. A task list. To find out the unfinished
    business, the task ID is generally not repeat
    all of the task ID into a set, it is assumed to ALL
    into a set of ID for all tasks completed, assumed to be COMPLETED, it is a subset of ALL
    ALL- COMPLETED = UNCOMPLETED

  5> The two groups randomly generated list of 10 numbers, the following requirements:
    Each number in the range [10,20]
    Statistics 20 numbers, a total number of different digital #set () to weight intersection of?
    carried out between the two groups, there are several different numbers? What are they? # symmetric difference
    compared between the two groups, the same numerals are there? What are they? # intersection of
    a = [1,9, 7,5,6,7,8,8,2,6]
    B = [1,9,0,5,6,4,8,3,2,3]



Guess you like

Origin www.cnblogs.com/zyybky/p/12605650.html