[Python study notes] 09 Yuan ancestor, dictionary, set common operations and dictionary underlying principles

This series of notes for learning Python for yourself, if there are errors, please correct me

Original tuple

The list is a variable sequence, and the elements in the list can be arbitrarily used since ancient times. The elements belong to the immutable sequence and cannot modify the elements in the original ancestor. Therefore, the original ancestor does not have methods for adding elements, modifying elements, and deleting elements.

Yuanzu supports the following operations:

  1. Index access
  2. Slice operation
  3. Connect operation
  4. Membership operations
  5. Comparison operation
  6. Count: the length of the original ancestor len() maximum value max() minimum value min() sum sum()

Creation of Yuanzu

  1. Create the ancestor through (), parentheses can be omitted

    a = (10,20,30) or a = 10,20,30

    If the ancestor has only one element, it must be followed by a comma, because the interpreter will interpret (1) as the integer 1 (1,) as the ancestor.

    a = (1,)
    type(a)
    
  2. Created by tuple()

    tuple (an iterable object)

    a = tuple()
    b = tuple("a,b,c")
    c = tuple([2,3,4])
    d = tuple(range(3))
    

    Summary: tuple() can receive lists, strings, other sequence types, and iterators to generate primitives.

    list() can receive primitives, strings, other sequence types, iterators, etc. to generate lists

Element access and counting of the ancestor

  1. The element of the ancestor cannot be modified

    a = (10,20,30,40)
    a[3] = 15 #报错 TypeError
    
  2. The element access of the ancestor is the same as that of the list, but the returned object is still the ancestor object

    a = (10,20,30)
    a[1]
    a[0:2]
    
  3. The sorting method of the list list.sorted() is to modify the original list object, but the original ancestor does not have this method. If you want to sort the original ancestor, you can only use the built-in function sorted(tupleObj) and generate a new list object

zip

zip (List 1, List 2...) combines the elements at the corresponding positions of multiple lists into a primitive ancestor, and returns this zip object.

a = [10,20,30]
b = [40,50,60]
c = [70,80,90]
d = zip(a,b,c)
list(d)#[(10,40,70),(20,50,80),(30,60,90)]

Generator deductive creation

From a formal point of view, the generator comprehension is similar to the list comprehension, except that the generator comprehension uses parentheses, and the list comprehension directly generates a list object. The generator comprehension generates not a list or a primitive ancestor, but a generator object .

We can use the generator object to convert it into a list or primitive ancestor, or use the __next__()method of the generator object to traverse, or use it directly as an iterator object. Regardless of the method, after the element access is over, if you need to revisit the element, you must recreate the generator object of the element.

s = (x*2 for x in range(5))#迭代器对象
s
tuple(s)
list(s) #此时为空 因为只能访问一次
s = (x*2 for x in range(5))
s.__next__() #访问下一个元素

Yuan Zu summary

  1. The core of Yuanzu is immutable sequence
  2. The access and processing speed of the original ancestor is faster than the list block
  3. Like integers and strings, the ancestor can be used as the key of a dictionary, and the list can never be used as the key of a dictionary.

dictionary

A dictionary is an unordered variable sequence of key-value pairs. Each element in the dictionary is a "key-value pair", including: "key object" and "value object". The key object can be used to quickly obtain, delete, and update Corresponding value object.

In the list, we find the corresponding object through the subscript number, and in the dictionary find the corresponding value object through the key object. Keys are arbitrary immutable objects, such as integer floating-point number string ancestors; but lists, dictionaries, and activations of these variable objects cannot be used as "keys", and "keys" cannot be repeated.

The value can be any data and can be repeated.

Dictionary creation

  1. We can create a dictionary object by {} dict()

    a = {
          
          'name':'slp','age':18,'job':'teacher'}
    b = dict(name='slp',age=18,job='teacher')
    a = dict([("name","18"),("age",18)])
    c = {
          
          } #空字典对象
    d = dict() #空字典对象
    
  2. Create a dictionary object by zip()

    k = ['name','age','job']
    v = ['slp',18,'teacher']
    d = dict(zip(k,v))
    
  3. Created by fromkeysValue is empty'S dictionary

    a = dict.fromkeys(['name','age','job'])
    

Access to dictionary elements

  1. Get the value through the key, if the key does not exist, throw an exception

    a = {
          
          'name':'slp','age':18}
    a['name'] #slp
    a['job'] #KeyError
    
  2. Get the value through the get() method, it is recommended to use, the point is: the specified key does not exist, return None, you can also set the default return object when the specified key does not exist

    a = {
          
          'name':'slp','age':18}
    a.get('name')
    
  3. List multiple key-value pairs

    a.items()
    
  4. List all keys, list all values

    a.keys()
    a.values()
    
  5. len() the number of key-value pairs

  6. Check if a key is in the dictionary

    'name' in a
    

Dictionary element addition, modification, deletion

  1. Add "key-value pairs" to the dictionary. If the "key" already exists, overwrite the old value, if it doesn't exist, add a new key-value pair

    a = {
          
          'name','slp'}
    a['address']='sx'
    
  2. Use update() to add all key-value pairs in the new dictionary to the old dictionary. If the key is repeated, it will be overwritten directly

    a = {
          
          'name':'slp','age':18}
    b = {
          
          'name':'hp','address':'sx'}
    a.update(b)
    
  3. To delete elements in the dictionary, you can use the del() method or clear() to delete all key-value pairs; pop() deletes the specified key-value pair and returns the corresponding "value object"

    a={
          
          'name':'slp',
     	'age':18}
    del(a['name'])
    
  4. popitem(): randomly delete and return the key-value pair. The dictionary is an unordered variable sequence, so there is no concept of the first element and the last element. popitem() pops up random items, because the dictionary does not have the last element or Other order concepts, if you want to delete one by one, this is very effective

    a = {
          
          'name':'slp','age':18}
    a.popitem()
    

Sequence unpacking

Sequence unpacking can be used for ancestors, lists, and dictionaries. Sequence unpacking allows us to easily assign values ​​to multiple variables,

x,y,z = (20,30,10)
x #20
(a,b,c)=(9,8,10)
a #9
[a,b,c]=[10,20,30]
c #30

When sequence unpacking is used in a dictionary, the default is to operate on keys; if you need to operate on key values, you need to use items(), if you need to operate on values, you need to use values()

s = {
    
    'name':'slp','age':18}
name,age = s
name #'name'
name,age=s.items()
name#{'name','slp'}

example

Table data is stored in dictionaries and lists, and accessed

Name age Salary city
Gao Xiaoyi 18 30000 Beijing
Gao Xiaoer 19 20000 Shanghai
Gao Xiaowu 20 10000 Shenzhen
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 14:06:22 2021

@author: sangliping
"""

r1 = {
    
    'name':'高小一','age':18,'salary':30000,'city':'北京'}
r2 = {
    
    'name':'高小二','age':19,'salary':10000,'city':'上海'}
r3 = {
    
    'name':'高小五','age':20,'salary':10000,'city':'深圳'}
tb = [r1,r2,r3]
print(tb)
# 获得第二行人的薪资
print(tb[1].get('salary'))
#打印表中所有的薪资
for i in range(len(tb)):
    print(tb[i].get('salary'))
    
# 打印表的所有数据
for i in range(len(tb)):    
    print(tb[i].get('name'),tb[i].get('age'),tb[i].get('salary'),tb[i].get('city'))
    

The core underlying principles of the dictionary

The core of the dictionary object is the hash table. The hash table is a sparse matrix. Each unit of the array is called a bucket. Each bucket has two parts: one is a reference to a key object and the other is a reference to a value object.

Since all buckets have the same structure and size, we can read the specified bucket by offset
Insert picture description here

Put a key-value pair into the underlying process of the dictionary

a = {
    
    }
a['name']='slp'

Suppose that after the dictionary a object is created, the array length is 8:
Insert picture description here

We need to put the key-value pair'name'='slp' into the dictionary object a. The first step is to calculate the hash value of the key, which can be calculated by hash() in python bin(hash('name'))

Since the length of the array is 8, we can take the rightmost 3 digits of the calculated hash value as the offset, that is 101, the decimal is 5. We check whether the bucket corresponding to offset 5 is empty, if it is empty, Put the key-value pair in. If it is not empty, take the right 3 digits in turn as the offset, which is 100. Check whether the bucket with offset 4 is empty, until you find an empty bucket and put the key-value pair Go in.
Insert picture description here

The underlying process of finding "key-value pairs" based on keys

When we call a.get('name'), we find the key-value pair according to the name and find the object. The first step is still to calculate the hash value.

Consistent with the underlying process algorithm of storage, the numbers at different positions of the hash value are also taken sequentially. Assuming that the length of the array is 8, we can take the rightmost 3 digits of the calculated hash value as the offset, and check whether the bucket corresponding to the decimal position is empty. If it is empty, return None. If it is not empty, then compare If they are equal, they will be returned as values. If they are not equal, they will be compared with other bits in turn. If it is still not found at the end, it returns None.
Insert picture description here

to sum up:

  1. Key must be hashable
    • Numeric string ancestors are all hashable
    • Custom objects need to support the following three points
      • Support hash() function
      • Support __eq__()methods to detect equality
      • If a==btrue, then hash(a)==hash(b)also true
  2. The dictionary has a huge overhead in memory, typical space for time
  3. Key query is fast
  4. Adding a new key to the dictionary may cause expansion and change the order of the keys in the hash table. Therefore, it is not necessary to modify the dictionary while traversing the dictionary.

set

The collection is disorderly and variable, and the elements cannot be repeated. In fact, the bottom layer of the collection is implemented by a dictionary. All elements of the collection are key objects in the dictionary, so they cannot be repeated and unique.

Collection creation and deletion

  1. Use {} to create a collection object, and use the add() method to add elements

    a = {
          
          3,5,7}
    a.add(9)
    
  2. Use set() to convert iterable objects such as lists and tuples into several. If there are duplicate values ​​in the data, only one is kept.

    a = ['a','b']
    b = set(a)
    
  3. remove() deletes the specified element, clear() clears the entire collection

Collection related operations

Like concepts in mathematics, python also provides operations such as union, intersection, and difference for sets

a = {
    
    1,2}
b={
    
    's','l'}
a|b #并集
a&b #交集
a-b #差集
a.union(b) #并集
a.intersection(b) #交集
a.difference(b) #差集

Search [Zixin] on WeChat or scan the QR code below to make friends and make progress together. The article is continuously updated. At present, I am organizing the study notes of Python hundred battles, and I look forward to more updates in the future.Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51656605/article/details/112667734