Small turtle Lesson Eleven: List: A "on steroids," the summary reflect array 2

2. If you want to remove an element from each end of the list and insert this element to the front of the list most, how would you do?

member = ['小甲鱼',88,'黑夜',90,'迷途',85,'易经',90,'斜阳',88]
member.insert(0,member.pop())
print(member)
#pop很巧妙的把末位删除且赋值给insert

3. Some fish oil more mischievous, he said I want to try list1 [-3: -1] will not complain, try actually know how to display [9, 7], which is how it happened?
A: Python list is very smart, even support negative indexes as:
list index .png

Here Insert Picture Description

Author: Innocence villains
link: https: //www.jianshu.com/p/cedfcd84b36b
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

4.4 during the fragmentation, we know that slice start and end positions need to be specified, but in fact there is another hidden settings: step size.

>>> list1[0:6:2]
[1, 2, 7]
那么依你推测,关于步长的知识点还有哪些(很多知识点都是通用的)?

Previously mentioned "simple" slicing valid here:

>>> list1[::2]
[1, 2, 7]
步长不能为0,要不就走不动了:
>>> list1 = [1, 3, 2, 9, 7, 8]
>>> list1 = [ : : 0 ]
SyntaxError: invalid syntax
>>>

Step can be negative, change direction (from the tail began to walk to the left):

>>> list1[::-2]
[8, 9, 3]
0

Author: Innocence villains
link: https: //www.jianshu.com/p/cedfcd84b36b
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

5. Turtle said the small class can use fragments to complete the list of copy list2 = list1 [:], that fact can you direct written list2 = list1 more simple it?
Not! Detailed explanation see https://www.jianshu.com/p/cedfcd84b36b

In short, according to a copy of a slice must be fixed, otherwise it will go wrong

Summary: use 1.pop and insert the
2 slices and a step of expression

Published 17 original articles · won praise 1 · views 362

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105223867