Python basic sequence

Python basic sequence

Sequence is a data storage method, which is used to store a series of data. In memory, sequence is a continuous memory space used to store multiple values. For example, an integer sequence [10,20,30,40] can be represented as follows:

Since everything in Python 3 is an object, it is actually stored in memory as follows: a = [10,20,30,40]

From the diagram, we can see that the address of the integer object is stored in the sequence, not the value of the integer object. The commonly used sequence structures in python are:
String, list, tuple, dictionary, set

1: list

List: used to store any number of data sets of any type.
List is a built-in variable sequence, which is an orderly and continuous memory space with multiple elements. Standard syntax format for list definition: a = [10,20,30,40]
Among them, 10, 20, 30, 40 are the elements of list a.
The elements in the list can be different and of any type. For example: a = [10,20, 'abc', True]
Summary of common methods for list objects:

Method Main points describe
list.append(x) Add elements Add element x to the end of the list
list.extend(aList) Add elements Add all elements of list a to the end of list
list.insert(index,x) Add elements Insert the element x at the position index specified in the list
list.remove(x) Delete elements Delete the first occurrence of the specified element x in the list
list.pop([index]) Delete elements Delete and return the element at index specified in list list, the default is the last element
list.clear() Delete all elements Deleting all elements of a list is not deleting list objects
list.index(x) Access element Returns the index position of the first x, and throws an exception if there is no x element
list.count(x) count Returns the number of times the specified element x appears in the list
len(list) List length Returns the number of elements in the list
list.reverse() Flip list Flip all elements in place
list.sort() sort Sort all elements in place
list.copy() Shallow copy Returns a shallow copy of a list object

Python's list size is variable, increasing or decreasing as needed.
String and list are sequence types. A string is a character sequence and a list is a sequence of any element.

1.1 creation of list

a = [10,20,'good','man']
b = []
c = list(range(10))
d = list(range(10,20,2))
e = list(range(16,5,-1))
f = list('Bruce, Jet, Jacky')

1.2 list derivation

a = [x for x in range(5)]
# [0, 1, 2, 3, 4]
b = [x*2 for x in range(5)]
# [0, 2, 4, 6, 8]
c = [x*2 for x in range(100) if x%9 == 0]
# [0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]
lis = [[x,x+1,x*x] for x in range(10)]
# [[0, 1, 0], [1, 2, 1], [2, 3, 4], [3, 4, 9], [4, 5, 16], [5, 6, 25], [6, 7, 36], [7, 8, 49], [8, 9, 64], [9, 10, 81]]

1.3 addition of list elements

When the list adds and removes elements, the list will automatically manage memory, which greatly reduces the burden of programmers. However, this feature involves a large number of movement of list elements, which is inefficient. Unless necessary, we usually only add or delete elements at the end of the list, which will greatly improve the operation efficiency of the list.

append() method: modify the object in place, which is the fastest way to add new elements at the end of the real list. Recommended.

a = [10, 20]
print(a)			# [10, 20, 30]

Plus operator operation: it is not the real tail to add elements, but to create a new list object; copy the elements of the original list and the elements of the new list to the new list object in turn. In this way, a large number of replication operations will be involved, and it is not recommended to operate on a large number of elements.

a = [10, 20]
print(id(a))		# 1756083343944
a = a+[50]
print(id(a))		# 1756089798088
print(a)			# [10, 20, 50]

extend() method: add all the elements of the target list to the end of the list. It belongs to in place operation and does not create a new list object.

a = [10, 20]
print(id(a))			# 2601761526344
print(id(a))			# 2601761526344
print(a)				# [10, 20, 30, 50]

insert() to insert an element: you can use the insert() method to insert the specified element into any specified position of the list object. This will cause all elements after the insertion position to move, which will affect the processing speed. When a large number of elements are involved, try to avoid using. There are also functions such as remove(), pop(), del(), which move elements after the operation position when deleting non tail elements.

a = [10, 20]
a.insert(1, 100)
print(a)			# [10, 100, 20]

Multiplication extension: use multiplication extension list to generate a new list. The new list element is a multiple repetition of the original list element.

a = [10, 20]
b = a * 3
print(b)			# [10, 20, 10, 20, 10, 20]

1.4 deletion of list elements

del delete: delete the element at the location specified in the list.

a = [100,200,888,300,400]
del a[1]
print(a)		# [100, 888, 300, 400]

pop() method: delete and return the specified location element. If no location is specified, the last element of the operation list will be defaulted.

a = [100,200,888,300,400]
print(a)		# [100, 200, 888, 300]

a = [100,200,888,300,400]
print(a)		# [100, 888, 300, 400]

remove() method: deletes the first occurrence of the specified element, throwing an exception if the element does not exist.

a = [100,200,888,300,400]
print(a)		# [100, 888, 300, 400]

a = [100,200,888,300,400]
# ValueError: list.remove(x): x not in list

1.5 list element access and count

Access elements directly through index: we can access elements directly through index. The index range is [0, list length - 1]. Beyond this range an exception will be thrown.

a = [100,200,888,300,400]
print(a[100])	# IndexError: list index out of range

Index() gets the index of the first occurrence of the specified element in the list: index() gets the index location of the first occurrence of the specified element. The syntax is: index(value,[start,[end]]). Where start and end specify the scope of the search.

a = [100,200,888,300,400]
print(a.index(200))			# 1
print(a.index(300, 3))		# 3
print(a.index(300, 3,7))	# 3

count(): gets the number of times the specified element appears in the list

a = [100,200,888,300,400]

len(): returns the length of the list, that is, the number of elements in the list.

a = [100,200,888,300,400]

Membership judgment: to determine whether the specified element exists in the list, we can use the count() method. If it returns 0, it means it does not exist. If it returns greater than 0, it means it exists. However, in general, we will use the more concise in keyword to judge, and directly return True or False.

a = [100,200,888,300,400]
print(100 in a)			# True
print(100 not in a)		# False

1.6 slice operation

Operation and description Example Result
[:] extract the entire list [10,20,30][:] [10,20,30]
[start:] from start to end of index [10,20,30][1:] [20,30]
[: end] know end-1 from the beginning [10,20,30][:2] [10,20]
[start:end] from start to end-1 [10,20,30,40][1:3] [20,30]
[start: end:step] extract from start to end-1, step is step [10,20,30,40,50,60,70][1:6:2] [20, 40, 60]
The last three [10,20,30,40,50,60,70][-3:] [50,60,70]
The fifth from the bottom to the third from the bottom 10,20,30,40,50,60,70][-5:-3] [30,40]
Step size is negative, reverse extraction from right to left [10,20,30,40,50,60,70][::-1] [70, 60, 50, 40, 30, 20, 10]

When slicing, the start offset and end offset are not in the range of [0, string length - 1], and no error will be reported.
If the start offset is less than 0, it will be regarded as 0; if the end offset is greater than length-1, it will be regarded as length-1.

1.7 traversal of list

a = [100,200,888,300,400]
for num in a:

1.8 list string conversion

String to list:

str1 = 'say hello python'
list1 = str1.split(' ')
print(list1, type(list1))
# ['say', 'hello', 'python'] <class 'list'>

List to string:

list1 = ['say', 'hello', 'python']
str1 = ' '.join(list1)
print(str1, type(str1))
# say hello python <class 'str'>

1.9 list de duplication

list1 = [33581, 3306, 10050, 22, 22]
def deleteDuplicatedElement(lis):
    return sorted(list(set(lis)), key = lis.index)
list2 = deleteDuplicatedElement(list1)
print(list1)    # [33581, 3306, 10050, 22, 22]
print(list2)    # [33581, 3306, 10050, 22]

1.10 sorted function sorting

The sorted() function sorts all the objects that can be iterated

The difference between sort and sorted:
Sort is a method applied to list. sorted can sort all objects that can be iterated.
The sort method of list returns to operate on the existing list, while the sorted method of the built-in function returns a new list instead of the original operation.

sorted(iterable, key=None, reverse=False)
# iterable -- iteratable object.
# key -- the element mainly used for comparison. There is only one parameter. The parameters of specific functions are taken from the iteratable objects. Specify an element in the iteratable objects to sort.
# Reverse -- collation, reverse = True descending, reverse = False ascending (default).
>>>sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]                      # Default is ascending

You can also use the list.sort() method of list. This method modifies the original list (the return value is None). Usually this method is not as convenient as sorted(). If you don't need the original list, the list.sort() method is a little more efficient.
Another difference is that the list.sort() method is only defined for list. The sorted() function can receive any iterable.

>>>sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]

Using key to sort in reverse order

>>>example_list = [5, 0, 6, 1, 2, 7, 3, 4]
>>> result_list = sorted(example_list, key=lambda x: x*-1)
>>> print(result_list)
[7, 6, 5, 4, 3, 2, 1, 0]

1.11 list elimination skills

li1 = [5, 10, '', 1, 2, '', 3, 4]
result = [x for x in filter(None, li1)]
# [5, 10, 1, 2, 3, 4]

1.12 filter function filtering

filter(function, iterable)

The filter() function is used to filter the sequence, filter out the unqualified elements, and return a new list of qualified elements.
Receive two parameters, the first is a function, and the second is a sequence. Each element of the sequence is passed as a parameter to the function for judgment. Return True or False, and put the element that returns True in the new list.

# Filter all odd numbers in the list
def is_odd(n):
    return n % 2 == 1
newlist = list(filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

Functions can also use lambda expressions instead:

new_list2 = list(filter(lambda n: n%2 ==1, [1,2,3,4,5,6,7,8,9]))

1.13 max, min, sum function

a = [40, 30, 20, 10]
print(max(a))		# Returns the largest element in the list
print(min(a))		# Returns the smallest element in the list
print(sum(a))		# Calculate the sum of all elements

2: tuple

A list belongs to a variable sequence. You can modify any element in the list. Tuples belong to immutable sequences, and elements in tuples cannot be modified. Therefore, tuples have no methods to add, modify and delete elements.
Therefore, we only need to learn how to create and delete tuples, and how to access and count elements in tuples. Tuples support the following operations:

  1. Index access
  2. Slicing operation
  3. Join operation
  4. Membership operations
  5. Comparison operation
  6. Count: tuple length len(), max(), min(), sum sum(), etc.

2.1 tuple creation

1: Create tuples through (). Parentheses can be omitted

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

If a tuple has only one element, it must be followed by a comma. This is because the interpreter interprets (1) as an integer 1 and (1,) as a tuple.

>>> a = (1)
>>> type(a)
<class 'int'>
>>> a = (1,)
>>> type(a)
<class 'tuple'>

2: Tuple creation through tuple()
Tuple (iteratable object)

b = tuple() #Create an empty tuple object
b = tuple("abc")
b = tuple(range(3))
b = tuple([2,3,4])

3: summary
tuple() can receive lists, strings, other sequence types, iterators, etc. to generate tuples.
list() can receive tuples, strings, other sequence types, iterators, and so on to generate lists.

2.2 access and count of tuple elements

1: Element of tuple cannot be modified

a = (1,2,3)
a[2] = 5
# TypeError: 'tuple' object does not support item assignment

2: The element access of a tuple is the same as that of a list, except that the tuple object is still returned

a = (20,10,30,9,8)
print(a[1], type(a[1]))		# 10 <class 'int'>
print(a[:2])				# (20, 10)

3: list.sorted() is the method to modify the original list object. Tuples do not have this method. If you want to arrange tuples
Only the built-in function sorted (tupeobj) can be used to generate new list objects

>>> a = (20,10,30,9,8)
>>> sorted(a)
[8, 9, 10, 20, 30]

2.3 zip function

Zip (Listing 1, Listing 2 )Combine the elements corresponding to multiple lists into tuples, and return the 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)]

2.4 generator derivation

Formally, generator derivation is similar to list derivation except that generator derivation uses parentheses.
The list object is directly generated by list derivation, and it is not a list or a tuple, but a generator object.
We can convert it into a list or tuple through the generator object. You can also use the generator object's  next  method for traversal, or directly as an iterator object. No matter how you use it, after element access, if you need to access the elements again, you must recreate the generator object.

>>> s = (x*2 for x in range(5))
>>> s
<generator object <genexpr> at 0x0000000002BDEB48>
>>> tuple(s)
(0, 2, 4, 6, 8)
>>> list(s) #The element can only be accessed once. The second time it was empty. Need to regenerate once
>>> s
<generator object <genexpr> at 0x0000000002BDEB48>
>>> tuple(s)
>>> s = (x*2 for x in range(5))
>>> s.__next__()
>>> s.__next__()
>>> s.__next__()

2.5 tuple summary

  1. The core characteristic of tuples is: immutable sequence.
  2. Tuples can be accessed and processed faster than lists.
  3. Like integers and strings, tuples can be used as keys to dictionaries, and lists can never be used as keys to dictionaries.

3: Dictionary

A dictionary is an unordered variable sequence of key value pairs. Each element in the dictionary is a key value pair, which includes key object and value object. You can quickly get, delete and update the corresponding value object through the key object.
In the list, we find the corresponding object through "subscript number". In the dictionary, find the corresponding value object through the key object. Key is any immutable data, such as integer, floating-point number, string, tuple. But: list, dictionary, collection, these variable objects can not be used as "key". And the "key" is not repeatable. The value can be any data and can be repeated.
The definition of a typical Dictionary:

a = {'name':'Bruce','age':18,'job':'programmer'}

3.1 dictionary creation

1: We can create dictionary objects by {}, dict()

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
b = dict(name = 'Bruce', age = 18, job = 'KongFu master')
c = dict([('name','Bruce'), ('age', 18)])
d = {}
e = dict()

2: Create dictionary objects with zip()

>>> k = ['name','age','job']
>>> v = ['gaoqi',18,'techer']
>>> d = dict(zip(k,v))
>>> d
{'name': 'gaoqi', 'age': 18, 'job': 'techer'}

3: Create a dictionary with null value through fromkeys

>>> a = dict.fromkeys(['name','age','job'])
>>> a
{'name': None, 'age': None, 'job': None}

3.2 access to dictionary elements

1: Get the value by [key]. Throw an exception if the key does not exist

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
print(a['sex'])			# KeyError: 'sex'

2: Get the value through the get() method. Recommended. The advantage is: the specified key does not exist, and returns None; you can also set
Specifies the object returned by default when the specified key does not exist. get() is recommended to get value object

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
print(a.get('sex', 'male'))		# If there is no sex key in the dictionary, enter male

3: List all key value pairs

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
# dict_items([('name', 'Bruce'), ('age', 18), ('job', 'KongFu master')])

4: Check whether the key is in the dictionary

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
print('name' in a)		# True

3.3 add, modify and delete dictionary elements

1: Add a key value pair to the dictionary. If the key already exists, the old key value pair will be overwritten; if the key does not exist, the new key value pair will be added

>>>a = {'name':'Bruce','age':18,'job':'programmer'}
>>> a['address']='No.1 courtyard of Xisanqi'
>>> a['age']=16
>>> a
{'name': 'Bruce', 'age': 16, 'job': 'programmer', 'address': 'No.1 courtyard of Xisanqi'}

2: Use update() to add all key value pairs in the new dictionary to the old dictionary object. If the key is duplicated, it will be overwritten directly

>>> a = {'name':'Bruce','age':18,'job':'programmer'}
>>> b = {'name':'Bruce Lee','money':1000,'sex':'Male'}
>>> a.update(b)
>>> a
{'name': 'Bruce Lee', 'age': 18, 'job': 'programmer', 'money': 1000, 'sex': 'Male'}

There is also a setdefault function for dictionary modification

a = {'name': 'Bruce', 'age': 18, 'job': 'KongFu master'}
a.setdefault('hobby', 'KongFu')		# Do nothing if the hobby key exists, otherwise set the value to KongFu
print(a)		# {'name': 'Bruce', 'age': 18, 'job': 'KongFu master', 'hobby': 'KongFu'}

3: Deletion of elements in dictionary
You can use del() method, or clear() to delete all key value pairs, pop() to delete the specified key value pairs and return the corresponding value object

>>> a = {'name':'Bruce','age':18,'job':'programmer'}
>>> del(a['name'])
>>> a
{'age': 18, 'job': 'programmer'}
>>> b = a.pop('age')
>>> b

popitem(): randomly delete and return the key value pair. Dictionaries are "unordered variable sequences", so there is no concept of the first and last element; popitem pops up random items, because dictionaries do not have the concept of "last element" or other related sequence. If you want to remove and process items one by one, this method is very efficient (because you don't need to get the list of keys first).

>>> a = {'name':'Bruce','age':18,'job':'programmer'}
>>> a.popitem()
('job', 'programmer')
>>> a
{'name': 'Bruce', 'age': 18}
>>> a.popitem()
('age', 18)
>>> a
{'name': 'Bruce'}

3.4 sequence unpacking

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

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

When sequence unpacking is used for dictionaries, the default is to operate on "keys"; if you need to operate on key value pairs, you need to use items(); if you need to operate on "values", you need to use values();

>>> s = {'name':'Bruce','age':18,'job':'teacher'}
>>> name,age,job=s 			# Default operation on key
>>> name
>>> name,age,job=s.items() 	# Operate on key value pair
>>> name
('name', 'Bruce')
>>> name,age,job=s.values() # Operate on values
>>> name

3.5 dictionary core underlying principle (important)

The core of a dictionary object is a hash table. A hash table is a sparse array (an array that always has blank elements), and each cell 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. Because all buckets have the same structure and size, we can read the specified bucket by offset.

The underlying process of putting a key value pair into a dictionary

>>> a = {}

Suppose that after the dictionary a object is created, the array length is 8:

To put the key value pair "name" = "gaoqi" into dictionary object a, the first step is to calculate the hash value of key "name". Python can be evaluated by hash().

>>> bin(hash("name"))

Because the array length is 8, we can take the rightmost 3 digits of the calculated hash value as the offset, "101", and the decimal system is the number 5. Let's check whether the bucket corresponding to offset 5 is empty. If it is empty, the key value pair is put in. If it is not empty, take 3 digits on the right as the offset, that is, "100", decimal is a number
4. Check whether the bucket with offset 4 is empty. Until the empty bucket is found, put the key value pair in. The flow chart is as follows:

The underlying process of finding key value pairs based on keys
We've learned how a key value pair is stored in an array. It's easy to understand how to get the value object according to the key object.

>>> a.get("name")

When we call a.get("name"), we find the "key value pair" according to the key "name", so as to find the value object "gaoqi".
First, we still need to calculate the hash value of the "name" object:

>>> bin(hash("name"))

It is consistent with the underlying process algorithm of storage, and it is also the number of different positions of hash value in turn. Assuming the array length is 8, we can take the rightmost 3 digits of the calculated hash value as the offset, that is, "101", and the decimal system is the number 5. Let's check whether the bucket corresponding to offset 5 is empty. If empty, returns None. If it is not empty, the key object of this bucket will be calculated and the corresponding hash value will be compared with our hash value, if it is equal. The corresponding value object is returned. If they are not equal, take other numbers in turn and recalculate the offset. After taking them one by one, they are still not found. Then return to None. The flow chart is as follows:

Usage Summary:

  1. Key must be hash able
    (1) Numbers, strings, tuples are hash able.
    (2) Custom objects need to support the following three points:
    1 supports hash() function
    2. It supports the detection of eq uality through the method of \.
    3 if a==b is true, then hash(a)==hash(b) is also true.
  2. Dictionaries are very expensive in memory, and typically change space for time.
  3. Fast key query
  4. Adding new to the dictionary may result in expansion and change of key order in the hash table. Therefore, do not modify the dictionary while traversing it.

3.6 dictionary traversal

1: Traversal key value pair

d = {'name': 'Andy', "age": 40}
for key in d.items():
# ('name', 'Andy')
# ('age', 40)

2: Traversal key

for key, value in d.items():
    print(key, value)
# name Andy
# age 40

3: Traverse key, find value through key

for key in d:
    print(key, d[key])
# name Andy
# age 40

4: set

The collection is unordered and variable, and the elements cannot be repeated. In fact, the bottom layer of the collection is the dictionary implementation, and all elements of the collection are key objects in the dictionary, so they cannot be repeated and unique.

4.1 set creation

myset = set()
myset1 = {1,2,3,4,5,6,5,4}
print(print(myset1), type(myset1))

4.2 convert to set type

set2 = set([1,2,3,4,5])
set3 = set((1,2,3,4,5))
set4 = set({1:2, 3:4})
set5 = set('abcdeftg')
print(set2, type(set2))  #{1, 2, 3, 4, 5} <class 'set'>
print(set3, type(set3))  #{1, 2, 3, 4, 5} <class 'set'>
print(set4, type(set4))  #After the {1, 3} < class' set '> dictionary is converted to set, only the key in the dictionary is saved
print(set5, type(set5))  #{'b', 'g', 'e', 'c', 'f', 't', 'a', 'd'} <class 'set'>

4.3 add single element

myset = {1,2,3,4}
print(myset)  # {1, 2, 3, 4, 5}
# Duplicate elements cannot be added, so the feature of set is de duplication

4.4 adding elements in batch

myset = {1,2,3,4,5,6}
myset.update([1,2,8,9])  # Must be an iterative object

4.5 deleting elements

myset = {1,2,3,4,5,6}
myset.remove(5)    # Delete the specified element and throw an exception if the specified element does not exist
myset.discard(14)  # Delete the specified element. If the specified element does not exist, do nothing
myset.pop()        # Delete the first element, return the deleted element
myset.clear()      # Empty all elements

4.6 copy set

myset = {1,2,3,4,5,6}
myset1= myset.copy()  # Deep replication

4.7 set operation

set1 = {1,2,3,4,5}
set2 = {1,2,7,8,9}
set3 = {1,2,3,4,5,6,7}

Subtraction operation:

print(set1 - set2)
print(set1.difference(set2))  # Elements in set1 and set2
# {3, 4, 5}
print(set2 - set1)
print(set2.difference(set1))  # Elements in set2 set but not in set1 set
# {8, 9, 7}

Intersection operation:

print(set1 & set2)  			# Intersection (elements in both sets)
# {1, 2}

Union operation:

print(set1 | set2)  # Union
# {1, 2, 3, 4, 5, 7, 8, 9}

Symmetric difference set:

print(set1 ^ set2)
print(set1.symmetric_difference(set2))  # Remove the common elements of two sets
# {3, 4, 5, 7, 8, 9}

Subsets and supersets:

print(set1.issubset(set3))    # True
print(set3.issuperset(set1))  # True

4.8 immutable set

fz = frozenset([1,2,3,4,5])
print(fz, type(fz))
# frozenset({1, 2, 3, 4, 5}) <class 'frozenset'>
Published 45 original articles, won praise 3, visited 1494
Private letter follow

Tags: Python Lambda less network

Posted on Fri, 14 Feb 2020 03:34:15 -0500 by sentback