Introduction to Python _ a brief introduction to the four basic types of data structures

  • 2020-06-01 10:07:19
  • OfStack

Data structure: in layman's terms, a container for storing large amounts of data. Here we mainly introduce four basic data structures of Python: list, dictionary, tuple and collection.

The format is as follows:

List: list = [val1 val2, val3, val4], with brackets;
Dictionary: dict = {key1: val1 key2: val2}, braces, and each element is key and val corresponding relation with the colon group;
Tuples: tuple = (val1 val2, val3, val4), parentheses;
Collection: set = {val1 val2, val3, val4}, curly braces.

1. The list:

list = [val1,val2,val3,val4]

The most striking feature of the list is:

Each element in the list is mutable;
The elements in the list are in order, that is, each element has a position;
The list can hold any object in Python.
Next, look at the table below.

Add:


1 list = [1,'dwd',3.6]
2 list.insert(0,'Python')
3 print(list)

You can add elements to a list using the insert method. The insert method needs to specify where to add and what to add. The actual position of the new element is the position before the specified position element. If the specified location does not exist, it is added to the end of the list by default.


1 list = [1,'dwd',3.6]
2 list[0:0] = [9]
3 #  【 0:0 】 refers to the list The first of 1 Insert a new element in each position 
4 list[3:3] = ['a']
5 #  【 3:3 】 refers to the list The first of 4 Insert a new element in each position 
6 print(list)

Both methods mentioned above are to add a single element, in addition to adding a single element, you can also add more than one element, using the extend method.


1 list_a = [1,'dwd',3.6]
2 list_b = ['Python',56,'game']
3 list_a.extend(list_b)
4 # extend Method is used at the end of the list 1 Secondary adjoin another 1 Multiple values in a list 
5 print(list_a)

Delete:


1 list = [1,'dwd',3.6]
2 list.remove('dwd')
3 print(list)

In addition to the remove method above, deleting elements from the list can also be declared using the del keyword:


1 list = [1,'dwd',3.6]
2 del list[0:2]
3 #  【 0:2 "Is to delete the first 1 And the first 2 Elements of position 
4 print(list)

Change:


1 list = [1,'dwd',3.6]
2 list[2] = 7
3 list[0] = 'start'
4 print(list)

If you want to replace an element in a list, you can simply reassign the element at a place in the list. list[2] refers to the third element in the list list.

Check:

The index of the list is similar to the index of the string mentioned in the previous article "Python introduction _ on sharding and indexing of strings and methods of strings". The index is divided into positive and negative indexes, which can be indexed from front to back or from back to front. Such as:


list = [1,'dwd',3.6]
print(list[1])
#  Print the first 2 Elements of position 
print(list[-1])
#  Print backward 1 Elements of position 
print(list[:2])
#  Print the first 1 And the first 2 An element 
print(list[1:])
#  Print the first 2 In the end 1 An element of 

But if you want to see the location of an element, this is not the way to do it, and the program will report an error. Because lists can only be indexed by location, you can't look up a location by an element.

2. The dictionary:

dict = {key1:val1,key2:val2}

Many concepts in the programming world are derived from life, and so are dictionaries. This data structure, like the dictionary we use 1, is constructed by "name-content". In Python, each element is the corresponding relation group of key and val with a colon, which is commonly referred to as a key-value pair.

The features of the dictionary are as follows:

The elements in the dictionary must be in the form of key-value pairs;

The key (key) cannot be repeated, while the value (val) can be repeated.

Keys are immutable and cannot be modified; Values are modifiable and can be any object.

Even if the dictionary has duplicate keys, they only appear once when printed. Such as:


1 dict = {'A':'art','B':'big','C':'cute','C':'cute'}
2 print(dict)

Let's look at the dictionary.

Add:

Dictionaries don't have a ready-made insert method to use like lists, but you can insert elements by default at the last location.


1 dict = {'A':'art','B':'big','C':'cute'}
2 dict['D'] = 'dictionary'
3 print(dict)

As mentioned above, you can use the extend method to add multiple elements to a list, and there is also the update method in the dictionary to add multiple elements.


1 dict = {'A':'art','B':'big','C':'cute'}
2 dict.update({'D':'dictionary','E':'exam'})
3 print(dict)

Delete:

You can also use the del keyword to remove an element from a dictionary.


1 list = [1,'dwd',3.6]
2 list[0:0] = [9]
3 #  【 0:0 】 refers to the list The first of 1 Insert a new element in each position 
4 list[3:3] = ['a']
5 #  【 3:3 】 refers to the list The first of 4 Insert a new element in each position 
6 print(list)
0

It is important to note that although dictionaries are bracketed, they are still bracketed when deleted.

Change:

If you want to modify an element in a dictionary, simply re-assign the key.


1 list = [1,'dwd',3.6]
2 list[0:0] = [9]
3 #  【 0:0 】 refers to the list The first of 1 Insert a new element in each position 
4 list[3:3] = ['a']
5 #  【 3:3 】 refers to the list The first of 4 Insert a new element in each position 
6 print(list)
1

Check:

When indexing in a dictionary, just like deleting 1, you use brackets to hold the key of the dictionary, which means that the dictionary element is indexed by the key.


1 list = [1,'dwd',3.6]
2 list[0:0] = [9]
3 #  【 0:0 】 refers to the list The first of 1 Insert a new element in each position 
4 list[3:3] = ['a']
5 #  【 3:3 】 refers to the list The first of 4 Insert a new element in each position 
6 print(list)
2

Sharding of strings was mentioned in the article Python introduction _ on the methods of sharding and indexing strings. In the dictionary, strings cannot be sharded.

3. The tuples

tuple = (val1,val2,val3,val4)

Tuples in Python are similar to lists, except that tuples are not modifiable, similar to lists in the stable version. Therefore, the methods of addition, deletion and modification that can be used in lists are not applicable in tuples, but elements in tuples can be indexed, similar to lists.


1 tup = (1,2,'s',7)
2 print(tup[0])
3 print(tup[-1])
4 print(tup[1:])
5 print(tup[:-2])

Collection of 4.

set = {val1,val2,val3,val4}

The idea of a set is somewhat close to a mathematical set. The elements in each set are unordered and non-repeating objects. We can judge the membership relationship of the data by the set, and sometimes we can subtract the repeating elements in the data structure by the set.

Collections cannot be sliced or indexed, and collection elements can be added and removed in addition to doing collection operations.


1 list = [1,'dwd',3.6]
2 list[0:0] = [9]
3 #  【 0:0 】 refers to the list The first of 1 Insert a new element in each position 
4 list[3:3] = ['a']
5 #  【 3:3 】 refers to the list The first of 4 Insert a new element in each position 
6 print(list)
4

And then when you use the set, you can expand the operation of the set.

A few tips on data structures will follow.

Operating environment: Python version, 3.6; PyCharm version, February 2016; Computer: Mac


Related articles: