当前位置:网站首页>Collections and dictionaries

Collections and dictionaries

2022-06-26 04:56:00 The story of Ula

Collections and dictionaries

One 、 aggregate (set):

Definition

  • 1. Composed of different elements
  • 2. Is an unordered, non repeating sequence of elements bi-
  • 3. The element of the collection must be of immutable type
    It can be used {} or set() Function to create a collection , Be careful : To create an empty collection, you must use set() instead of {}, because {} Is used to create an empty dictionary .
1. Create format :
par = {
    value1,value2,value3,...}
 perhaps 
set(value)

Add elements to the collection

s.add(a)#a  If a Already exist , No operations 
 perhaps 
s.update(a)#a Can be a list , Tuples , Dictionary, etc .

a It can be more than one , Separated by commas .

2. Remove elements :
s.remove(a)# take a Remove from collection , If the element does not exist , Will report a mistake .
s.discard(a)# take a From the collection s Remove from , If the element does not exist , And there will be no mistakes 
s.pop()# Randomly delete an element in the collection .

set A collection of pop Method will sort the collection out of order , Then delete the first element on the left of the unordered set .

3. Calculate collection elements :
len(s)# Computing sets s The number of elements in 
4. Empty the set :
s.close()# Empty the set s
5. Determine whether the element exists in the collection :
a in s# Element of judgement a  Is it gathering  s  in , There is returned True, There is no return False.
6. Methods built into the collection
Method describe
add() Add an element to the collection
clear() Remove all elements from the collection
copy() Copy a collection
difference() Returns the difference set of multiple sets
difference update() Remove elements from collection , This element also exists in the specified collection
discard() Delete the elements specified in the collection
intersection() Returns the intersection of sets
intersection update() Returns the intersection of sets
isdisjoint() Determine whether two sets contain the same elements , No return True, Otherwise return to False
issubset() Determine whether the specified set is a subset of the method parameter set
issuperset() Judge whether the method parameter set is a subset of the specified set
pop() Remove elements at random
remove() Removes the specified element
symmetric difference() Returns the set of elements that are not repeated in two sets
symmetric difference update() Remove the same elements from the current collection in another specified collection , And insert different elements of another specified collection into the current collection
union() Returns the union of two sets
update() Crisis and adding elements

Two 、 Dictionaries (dictionary):

  • Python Built in dictionary :dict Support for ,dict The entire dictionary. Dictionary is another variable container model , And can store any type of object .
  • Dictionaries are extremely fast to find .
  • The key must be unique , Value doesn't have to be
  • Values can be any type , The key must be immutable
  • len(): Return dictionary key : The number of value pairs .
  • d[k]: Return the value corresponding to the keyword .
  • d[k] = v: Associate values to key values k On .
  • del d[k]: The delete key value is k The item .
  • key in d: Key value key Whether in d in , Is to return True, Otherwise return to False
1. The basic format of the dictionary :
d = {
    key1:value1,key2:value2}

Key to dictionary (key) Must be immutable , value (key) It can be any data type . The dictionary is out of order , Print every time , The order will change

2. Dictionary operation :

len(dict): Count the number of dictionary elements , That is, the total number of .
str(dict): Output Dictionary , In a printable string .
type(variable): Returns the type of variable entered , If the variable is a dictionary , Return the dictionary type .

3. Dictionary method :

del(key): Delete elements from Dictionary .
clear(): Empty dictionary .
del Dictionary name : Delete Dictionary .
iteams(): Loop through the dictionary key and value.
keys(): Loop through all the in the dictionary key.
values(): Loop through all the in the dictionary value.
copy(): Shallow copy
fromkeys(): Create a new dictionary ,fromkeys(*args,**kwargs) With *args Make a dictionary key ,**kwargs Is the corresponding initial value of all keys .
pop(): Delete specified key.
popitem(): Randomly delete a key value pair .
setdefault(): Add a new key to the dictionary , There is no execution , If it doesn't exist, increase and return the value corresponding to the key .
update(): Update Dictionary .

原网站

版权声明
本文为[The story of Ula]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180509133048.html