list用法

1 查看两个list的交并差

http://www.cnblogs.com/jlf0103/p/8882896.html

2 将一个list中的元素反序

reversed()前要加list

https://www.runoob.com/python3/python3-func-reversed.html

3 Python新建list的方法直接  a = [ ] 即可,不可用 a = list[ ],在Python中list()是一个函数,用于将tuple转化为list

tuple与list的互化: https://www.cnblogs.com/cnhkzyy/p/8679124.html

list()也可用于快速生成新的list  参考:https://www.cnblogs.com/chendai21/p/8125422.html

4 list中插入元素有三种方法

参考:https://blog.csdn.net/always1898/article/details/79550389

5 快速生成一个list等差数列的方法,画图时会用到

a = list(range(20))

 6 生成一个任意长度的且值全部相同的list

a = [None] * 10
print(a)
b = [0] * 10
print(b)
# [None, None, None, None, None, None, None, None, None, None]
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

 7 list中删除元素

a. 只删除一个元素时.

有remove,del,pop三个方法,remove是删除的元素,del和pop删除的是索引对应的元素.

a = [1,2,3,4,5,6,7,8,9]
# remove直接删除的是元素
a.remove(1)
print(a)
# [2, 3, 4, 5, 6, 7, 8, 9]
# del与remove不同,del是按索引删的,即它删除的是索引对应的元素,如下删除的是元素4
del a[2]
print(a)
# [2, 3, 5, 6, 7, 8, 9]
# pop与del用法相同,也是删除的索引对应的元素
a.pop(3)
print(a)
# [2, 3, 5, 7, 8, 9]
View Code

b. 删除多个元素时.

posted on 2019-04-25 11:41  吃我一枪  阅读(197)  评论(0编辑  收藏  举报

导航