My friends, for reprint please indicate the source: blog.csdn.net/jiangjunsho…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

Because lists are sequences, indexing and sharding operations are essentially the same for lists as they are for strings.

>>> L = ['spam','Spam','SPAM!'] >>> L[2] # Offsets start at zero 'SPAM! ' >>> L[-2] # Negative: count from the right 'Spam' >>> L[1:] # Slicing fetches sections ['Spam','SPAM!']Copy the code

Because lists (and other object types) can be nested within lists, it is sometimes necessary to use several index operations together to drill down into data structures. For example, one of the simplest ways is to represent it as a matrix (multidimensional array), the Python equivalent of a list with nested sublists. Here we look at a 3×3 two-dimensional array based on a list:

= > > > matrix [[1, 2, 3], [4 and 6], [7,8,9]]Copy the code

If you use the index once, you get a whole row (in effect, a nested sublist), and if you use the index twice, you get one of the items in a row:

> > > matrix [1] [4 and 6] > > > matrix [1] [1] 5 > > > matrix [2] [0] 7 > > > = matrix [[1, 2, 3],... (4 and 6), [7,8,9]] >>> matrix[1] 5Copy the code