White Science Python (10): basic data structure (list) (lower)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

List sliced

When it comes to the list of slices students do not know you have not remembered before the string sections we have said, the students thought up three seconds of silence the memory of it for themselves.

Just kidding :) can return to the previous article to see.

Refers to the list of sections cut out part of the list of them.

Syntax: List [starting index: Index termination: step interval]

Note: This index is terminated and string sections is the same, and will not get to.

Directly following the code parsing has been added in a note.

In fact, I was lazy, I do not want to open one by one to write, to hit me ah~

list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# 省略步长时默认为 1
print(list1[3:8])
# 步长为 2
print(list1[3:8:2])
# 从索引 3 开始取到最后
print(list1[3:])
# 从头开始取,取到索引 8 ,并且索引 8 娶不到
print(list1[:8])
# 取所有,步长为 3
print(list1[::3])
# 从索引 1 开始,取到倒数第 2 个,并且倒数第 2 个 取不到
print(list1[1:-2])
# 取所有
print(list1[:])
# 取逆序列表
print(list1[::-1])
# 取逆序,并且步长为 2
print(list1[8:1:-2])

Execution results are as follows:

[3, 4, 5, 6, 7]
[3, 5, 7]
[3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 3, 6, 9]
[1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[8, 6, 4, 2]

A list of commonly used methods

We are here to show you:

list.append(obj)

Add new object at the end of the list

list1.append("Python")
print(list1)

The results are as follows:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'Python']

list.count(obj)

The number of times an element statistics appear in the list

list2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

list2.append("Python")
list2.append("Python")
list2.append("Python")
list2.append(1)
print(list2.count("Python"))
print(list2.count(1))

The results are as follows:

3
2

We were here a string of statistics  "Python" and figures  1 the number of occurrences.

list.extend(seq)

A plurality of values ​​in another sequence added at the end of the list of one-time (with a new list of the original extended list)

list1.extend(list2)

print(list1)

The results are as follows:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'Python', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'Python', 'Python', 'Python', 1]

I can see list1 list2 after expansion, while containing all the data in list1 and list2.

extend and append very much like, but may extend once add a list, and append can only add an element.

list.index(obj)

A value index to find the location of the first match from the list

print(list1.index("Python"))

The results are as follows:

10

list.insert(index, obj)

Inserting objects list

inserting index value index, obj is the element to be inserted.

list1.insert(0, "Hello")
print(list1)

list3 = [0, 1, 2]
list4 = [2, 2]
list3.insert(1, list4)
print(list3)

The results are as follows:

['Hello', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'Python', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'Python', 'Python', 'Python', 1]
[0, [2, 2], 1, 2]

Of course, when the insert may be a separate element or may be a list.

list.pop([index=-1])

移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

list3.pop()
print(list3)

list3.pop(1)
print(list3)

结果如下:

[0, [2, 2], 1]
[0, 1]

同样在移除索引的时候,可以移除一个元素,也可以移除一个列表。

list.remove(obj)

移除列表中某个值的第一个匹配项

list5 = [1, 2, 3, 4, 4, 5]
list5.remove(4)
print(list5)

结果如下:

[1, 2, 3, 4, 5]

list.reverse()

反向列表中元素

list5.reverse()
print(list5)

结果如下:

[5, 4, 3, 2, 1]

虽然我们刚才用步长为 -1 的形式同样做到了列表逆序,但是这两个操作是不一样的。

list5 = [1, 2, 3, 4, 4, 5]
list5.remove(4)
print(list5)
print(id(list5))
list5.reverse()
print(list5)
print(id(list5))
print(id(list5[::-1]))

结果如下:

[1, 2, 3, 4, 5]
2629333420040
[5, 4, 3, 2, 1]
2629333420040
2629333420104

各位同学看明白了,通过步长获得的逆序列表实际上是一个新的列表,在内存的地址发生了变动,而通过 list.reverse() 打印的逆序列表还是这个列表本身,它的内存地址并未发生改变。

list.sort( key=None, reverse=False)

对原列表进行排序

list6 = [2, 5, 1, 9, 6, 3]
list6.sort()
print(list6)
list6.sort(reverse=True)
print(list6)

结果如下:

[1, 2, 3, 5, 6, 9]
[9, 6, 5, 3, 2, 1]

怎么样,对列表进行正序和逆序排序是不是很简单。

后面还有两个方法 list.clear() (清空列表) 和 list.copy() (复制列表)我就不做演示,大家可以自己动手尝试一下,有不清楚的可以在公众号后台留言问我。

示例代码

本系列的所有代码小编都会放在代码管理仓库 Github 和 Gitee 上,方便大家取用。

示例代码-Github

示例代码-Gitee

转载声明:本博客由极客挖掘机创作,采用  CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。

 

Guess you like

Origin www.cnblogs.com/aliswell2king/p/11753919.html