Python automatic operation and maintenance development -- Basic slice Application & & list function

1. Slice application (1) copy list: what's the difference between directly assigning a new list and slicing a new list? Use the method of assign...

1. Slice application

(1) copy list: what's the difference between directly assigning a new list and slicing a new list?

Use the method of assigning a new list directly and looking at the location in memory. In this way, the location of two lists in memory is the same, and one list changes and the other changes

>>> a = [1, 2, 3] >>> b = a >>> id(a) 139827490542536 >>> id(b) 139827490542536 >>> a[0] = 0 >>> b[0] 0 >>> b [0, 2, 3]

The list obtained by slicing is a brand new list, which will not change with the previous list and the location of the list in memory

>>> c = a[:] >>> c [0, 2, 3] >>> a [0, 2, 3] >>> a[0] = 1 >>> a [1, 2, 3] >>> c [0, 2, 3] >>> id(a) 139827490542536 >>> id(c) 139827490544840

(2) reverse list

The list will be reversed when the list is in reverse order with step = -1

>>> a [1, 2, 3] >>> a[::-1] [3, 2, 1]

(3) get list elements whose index numbers are even

>>> a [1, 2, 3] >>> a[::2] [1, 3]

(4) get list elements whose index numbers are all odd

>>> a [1, 2, 3] >>> a[1::2] [2]

(5) use slices to add, delete and modify the list

Slice a list into an empty list

>>> a [1, 2, 3, 1, 2, 4] >>> a[:] = [] >>> a []

Changing the values of list elements by slicing

>>> a [1, 2, 3, 5, 6] >>> a[2:3] [3] >>> a[2:3] = [4,1] >>> a [1, 2, 4, 1, 5, 6]

Delete list elements by slicing

>>> a [1, 2, 4, 1, 5, 6] >>> a[2:5] = [] >>> a [1, 2, 6]

2. Slice function

What are the functions for viewing slices?

dir(list)

View help for using functions?

help(list. Function name)

(1) append function

>>> a [1, 2, 6] >>> a.append(5) >>> a [1, 2, 6, 5]

(2) elements of the case list of the clear function

>>> a [1, 2, 6, 5] >>> a.clear() >>> a []

What's the difference between a.clear() and a = []?

Position in memory changed

>>> a [1, 2, 5, 6] >>> id(a) 139827490570312 >>> a = [] >>> id(a) 139827490545224

(3) copy function copies a list

>>> a = [1, 2] >>> b = a.copy() >>> b [1, 2] >>> id(a) 139827490570312 >>> id(b) 139827490545480

(4) count function counts the number of value occurrences

>>> a [1, 2, 1, 2] >>> a.count(1) 2 >>> a.count(2) 2

(5) extend function extension element

>>> a [1, 2, 1, 2] >>> a.extend('456') >>> a [1, 2, 1, 2, '4', '5', '6']

What's the difference between extend function and append function?

>>> a.extend('456') >>> a [1, 2, 1, 2, '4', '5', '6'] >>> a.append(456) >>> a [1, 2, 1, 2, '4', '5', '6', 456]

extend is to split the things to be added into elements to be added at the back, and append is to take the things to be added as a whole as a whole

(6) index function is used to get the index number of elements

>>> a [1, 2, 1] >>> a.index(1) 0 >>> a.index(2) 1

(7) insert function insert element (insert before the specified index number)

>>> a [1, 2, 1] >>> a.insert(1,3) >>> a [1, 3, 2, 1]

(8) pop function remove and return the element value (the default is to delete the last element, you can specify the deleted element, and you need to specify the index number)

A. pop (index number) does not write the index number. The default is to delete the last one

>>> a [1, 3, 2, 1] >>> a.pop() 1 >>> a [1, 3, 2]

(9) remove function removes the first matched value

>>> a [1, 3, 2, 1] >>> a.remove(1) >>> a [3, 2, 1]

(10) reverse function

>>> a [3, 2, 1] >>> a.reverse() >>> a [1, 2, 3]

What is the difference between reverse function and slice reversal?

reverse changes the original list

(11) sort function

>>> a[2:3] = [6,5] >>> a [1, 2, 6, 5] >>> a.sort() >>> a [1, 2, 5, 6]


2 December 2019, 00:22 | Views: 5577

Add new comment

For adding a comment, please log in
or create account

0 comments