05 data type 2 list of Python

  

In the last lesson, we talked about Python values and strings

In the process of programming, we often encounter the situation of using a lot of data quantity. There must be a corresponding variable to deal with each data quantity. If each variable has to be defined separately, it is very cumbersome. Using list can solve this problem

Python's numeric type List, that is, List

Python's list is similar to the concept of array in other languages, but it is quite different from the concept of array in other languages

The definition of array in C and Java is like this. A collection that stores multiple values of the same type is called an array.

But in Python, lists can store many types of data

For example, a list of common data types

In [3]: My_List = [10,20,30]

In [4]: My_List
Out[4]: [10, 20, 30]

  

List with multiple data types

In [5]: My_Profile = ["susmote", 18, 2018, 0.5]

In [6]: My_Profile
Out[6]: ['susmote', 18, 2018, 0.5]

  

Get values in list

In [8]: My_List = [10, 20, 30]

In [9]: My_List[0]
Out[9]: 10

In [10]: My_List[1]
Out[10]: 20

  

Of course, the list can also be obtained in reverse order

In [11]: My_List = [10, 20, 30]

In [12]: My_List[-2]
Out[12]: 20

In [13]: My_List[-1]
Out[13]: 30

  

In the previous section, we talked about slicing. Here we can also slice the list to get its values

In [14]: My_List = [10, 20, 30, 40, 50, 60]

In [15]: My_List[1:2]
Out[15]: [20]

In [16]: My_List[0:2]
Out[16]: [10, 20]

  

Of course, you can use the negative sign reverse order to get the list value

In [28]: My_List = [10, 20, 30, 40, 50, 60]

In [29]: My_List[-3:]
Out[29]: [40, 50, 60]

  

You can also add directly to the list

In [30]: My_List = [10, 20, 30, 40, 50, 60]

In [31]: My_List + [70]
Out[31]: [10, 20, 30, 40, 50, 60, 70]

  

The list supports substitution. Unlike strings, which are immutable, the list is a variable type

In [6]: word = ["a", "b", "c", "d", "f", "f"]

In [7]: word
Out[7]: ['a', 'b', 'c', 'd', 'f', 'f']

In [8]: word[4] = 'e'

In [9]: word
Out[9]: ['a', 'b', 'c', 'd', 'e', 'f']

  

If you want to add data to a list, you can use the append() method

In [10]: My_List = [1, 2, 3, 4, 5]

In [11]: My_List
Out[11]: [1, 2, 3, 4, 5]

In [12]: My_List.append(6)

In [13]: My_List
Out[13]: [1, 2, 3, 4, 5, 6]

  

To change the data, you can also use slice replacement in batch, or you can use replacement to clear the values in the list

In [14]: word = ['a', 'b', 'c', 'd', 'e']
In [15]: word
Out[15]: ['a', 'b', 'c', 'd', 'e']
In [18]: word[1:2] = ['B']
In [19]: word
Out[19]: ['a', 'B', 'c', 'd', 'e']

  

In [19]: word
Out[19]: ['a', 'B', 'c', 'd', 'e']

In [20]: word = []

In [21]: word
Out[21]: []

  

Get the length of the entire list, or use the built-in method len()

In [22]: word = ['a', 'b', 'c', 'd', 'e']

In [23]: len(word)
Out[23]: 5

  

Nesting of lists, in which lists can be placed again to form multi list nesting

In [25]: a = [1, 2, 3]

In [26]: b = [4, 5, 6]

In [27]: my_list = [a, b]

In [28]: my_list
Out[28]: [[1, 2, 3], [4, 5, 6]]

  

Get values in nested list

In [31]: my_list
Out[31]: [[1, 2, 3], [4, 5, 6]]

In [32]: my_list[0][1]
Out[32]: 2

In [33]: my_list[1][1]
Out[33]: 5

  

This is the end of the list. I may say that other knowledge points are supplemented later

Tags: Python Programming Java

Posted on Sun, 05 Apr 2020 03:26:59 -0700 by yong