列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。

1.创建list

1 list1=[]
2 print(list1)
3 print(type(list1))

返回:

[]
<class 'list'>

 

2.list的attribute

图片上面的方法分别为:

list.append()

向list中添加元素;

 1 list1=[]
 2 
 3 list.append(list1,1)
 4 
 5 list1
 6 Out[3]: [1]
 7 
 8 list1.append('2')
 9 
10 list1
11 Out[5]: [1, '2']

 

list.clear()

清除列表内所有元素;

 1 list.clear(list1)
 2 
 3 list1
 4 Out[7]: []
 5 
 6 list1.append('a')
 7 
 8 list1
 9 Out[9]: ['a']
10 
11 list1.clear()
12 
13 list1
14 Out[11]: []

 

list.copy()

浅拷贝list内的第一层的列表元素;

 1 list1=[1,2,[3,4]]
 2 
 3 list1
 4 Out[14]: [1, 2, [3, 4]]
 5 
 6 list2=list1.copy()
 7 
 8 list2
 9 Out[16]: [1, 2, [3, 4]]
10 
11 list1[0]=0
12 
13 list1
14 Out[18]: [0, 2, [3, 4]]
15 
16 list2
17 Out[19]: [1, 2, [3, 4]]
18 
19 #浅拷贝,list1发生变化时,list2不发生变化;
20 
21 list1[2][0]=['a']
22 
23 list1
24 Out[22]: [0, 2, [['a'], 4]]
25 
26 list2
27 Out[23]: [1, 2, [['a'], 4]]
28 
29 #浅拷贝,list1中的列表元素发生变化时,list2也发生变化;

注意:想要实现深拷贝

 1 import copy 
 2 
 3 list3=copy.deepcopy(list1)  #copy.copy()还是浅拷贝
 4 
 5 list3
 6 Out[27]: [0, 2, [['a'], 4]]
 7 
 8 list1[2][0]=0
 9 
10 list1
11 Out[29]: [0, 2, [0, 4]]
12 
13 list3
14 Out[30]: [0, 2, [['a'], 4]]

   

list.count()

列表内元素的计数:

1 list1
2 Out[37]: [0, 2, [0, 4]]
3 
4 list1.count(0) #等价list.count(list1,0)
5 Out[38]: 1

 

list.extend()

列表的拓展:

 1 list1=[1,2]
 2 
 3 list2=[3,4]
 4 
 5 list1.extend(list2)
 6 
 7 list1
 8 Out[53]: [1, 2, 3, 4]
 9 
10 list1.extend([5,6,7,[1,2]])
11 
12 list1
13 Out[55]: [1, 2, 3, 4, 5, 6, 7, [1, 2]]

 

list.index()

列表的索引,通过值取下标:

1 list1
2 Out[56]: [1, 2, 3, 4, 5, 6, 7, [1, 2]]
3 
4 list1.index(1)
5 Out[57]: 0
6 
7 list1.index([1,2])
8 Out[58]: 7

 

list.insert()

列表中插入新元素,可以指定相应的下表:

1 list1
2 Out[59]: [1, 2, 3, 4, 5, 6, 7, [1, 2]]
3 
4 list1.insert(0,[1])
5 
6 list1
7 Out[62]: [[1], 1, 2, 3, 4, 5, 6, 7, [1, 2]]

 

list.mro()

???基本报错,并且在list1.attribute中没有该方法,但是在本文的截图中却有该方法。目前未找到相关文档,暂缓。

 1 list..mro(list1)
 2   File "<ipython-input-66-13590575fc0e>", line 1
 3     list..mro(list1)
 4          ^
 5 SyntaxError: invalid syntax
 6 
 7 
 8 list.mro(list1)
 9 Traceback (most recent call last):
10 
11   File "<ipython-input-67-4e06214a8533>", line 1, in <module>
12     list.mro(list1)
13 
14 TypeError: mro() takes no arguments (1 given)
15 
16 
17 list.mro()
18 Out[68]: [list, object]
19 
20 list1.mro()
21 Traceback (most recent call last):
22 
23   File "<ipython-input-69-52a1ac213b6a>", line 1, in <module>
24     list1.mro()
25 
26 AttributeError: 'list' object has no attribute 'mro'

 

list.pop()

删除列表中的一个元素,默认值是删除最后一个元素:

1 list1
2 Out[72]: [[1], 1, 2, 3, 4, 5, 6, 7]
3 
4 list1.pop()  #等价list.pop(list1)
5 Out[73]: 7
6 
7 list1
8 Out[74]: [[1], 1, 2, 3, 4, 5, 6]

如果想要实现删除指定位置的元素。可以通过指定索引下标的位置实现。

1 list1
2 Out[74]: [[1], 1, 2, 3, 4, 5, 6]
3 
4 list1.pop(0)
5 Out[75]: [1]
6 
7 list1
8 Out[76]: [1, 2, 3, 4, 5, 6]

 

list.register()

无该属性,报错。

 1 list1.register()
 2 Traceback (most recent call last):
 3 
 4   File "<ipython-input-77-2af250941f18>", line 1, in <module>
 5     list1.register()
 6 
 7 AttributeError: 'list' object has no attribute 'register'
 8 
 9 
10 list.register()
11 Traceback (most recent call last):
12 
13   File "<ipython-input-78-04aa76a73770>", line 1, in <module>
14     list.register()
15 
16 AttributeError: type object 'list' has no attribute 'register'

 

list.remove()

移除列表中的元素:

1 list1
2 Out[80]: [1, 2, 3, 4, 5, 6]
3 
4 list1.remove(1)  #等价list.remove(list1,2)
5 
6 list1
7 Out[82]: [2, 3, 4, 5, 6]

 

list.reverse()

列表中的元素反转:

1 list1
2 Out[84]: [3, 4, 5, 6]
3 
4 list1.reverse() #等价list.reverse(list1)
5 
6 list1
7 Out[86]: [6, 5, 4, 3]

 

list.sort()

列表中的元素按照ascii码的顺序进行排序:

 

1 list1
2 Out[92]: ['!', '^', 'A', 'a']
3 
4 list1.sort()
5 
6 list1
7 Out[94]: ['!', 'A', '^', 'a']

 

3.删除列表

1 del list1
2 
3 list1
4 Traceback (most recent call last):
5 
6   File "<ipython-input-106-8304c57391a2>", line 1, in <module>
7     list1
8 
9 NameError: name 'list1' is not defined

 

posted on 2017-06-13 17:06  Zoe233  阅读(129)  评论(0编辑  收藏  举报