赞
踩
>>> a=[1,2,3,4]
>>> a=["hello","world","haha"]
>>> type(a)
<class 'list'>
>>> a=[1,2,"hello","world"]
>>> type(a)
<class 'list'>
>>> a=[1,2,"hello","world",[4,5,6]]
>>> type(a)
<class 'list'>
>>> a=[1,2,3,4,5]
>>> a[0]
1
>>> a[4]
5
>>> a[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> a=[1,2,3,4,5]
>>> a[-1]
5
>>> a[-2]
4
>>> a[-5]
1
>>> a[-6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> a=[1,2,3,4,5,6,7,8,9]
>>> a[0:3]
[1, 2, 3]
>>> a[5:8]
[6, 7, 8]
>>> a[0:8]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> a=[1,2,3,4,5,6,7,8,9]
>>> a[:4]
[1, 2, 3, 4]
>>> a[5:]
[6, 7, 8, 9]
>>> a[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a=[1,2,3,4,5]
>>> a[3:10]
[4, 5]
>>> a=[1,2,3,4,5]
>>> a[4:1]
[]
>>> a[-1:4]
[]
>>> a=[1,2,3,4,5,6,7,8,9]
>>> a[0:5:2]
[1, 3, 5]
>>> a[2:8:4]
[3, 7]
>>> a=[1,2,3,4]
>>> len(a)
4
>>> a=[]
>>> len(a)
0
>>> a=[1,2,3,4]
>>> max(a)
4
>>> a=["hello","world","ok"]
>>> max(a)
'world'
>>> a=[1,2,3,4]
>>> min(a)
1
>>> a=["hello","world","ok"]
>>> min(a)
'hello'
>>> a=[1,2,3,4,5]
>>> sum(a)
15
>>> a=[1,2,3,4,5]
>>> 0 in a
False
>>> 0 not in a
True
>>> 1 in a
True
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> a=[1,2,3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> a.append("hello")
>>> a
[1, 2, 3, 4, 'hello']
>>> a=[1,2,3]
>>> a.clear()
>>> a
[]
>>> a=[1,2,3,[5,6,7]]
>>> b=a.copy()
>>> a
[1, 2, 3, [5, 6, 7]]
>>> b
[1, 2, 3, [5, 6, 7]]
>>> a[3].append(8)
>>> a
[1, 2, 3, [5, 6, 7, 8]]
>>> b
[1, 2, 3, [5, 6, 7, 8]]
>>> a[1]=100
>>> a
[1, 100, 3, [5, 6, 7, 8]]
>>> b
[1, 2, 3, [5, 6, 7, 8]]
>>> a=[1,2,3,4,3,2,3,4,3,2,1,3,4,2]
>>> a.count(2)
4
>>> a.count(3)
5
>>> a.count(0)
0
>>> a=[1,2,3,4,5,6,7,8,9,0]
>>> a.index(1)
0
>>> a.index(1,3,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 1 is not in list
>>> a.index(5,3,5)
4
>>> a=[1,2,3,4,5]
>>> a.insert(0,100)
>>> a
[100, 1, 2, 3, 4, 5]
>>> a.insert(3,300)
>>> a
[100, 1, 2, 300, 3, 4, 5]
>>> a.insert(7,700)
>>> a
[100, 1, 2, 300, 3, 4, 5, 700]
>>> a=[1,2,3,4,5,6]
>>> b=a.pop()
>>> b
6
>>> a
[1, 2, 3, 4, 5]
>>> b=a.pop(3)
>>> b
4
>>> a
[1, 2, 3, 5]
>>> a=[1,2,3,4,3,2,1,2,3]
>>> a.remove(2)
>>> a
[1, 3, 4, 3, 2, 1, 2, 3]
>>> a=[1,2,3,4,5]
>>> a.reverse()
>>> a
[5, 4, 3, 2, 1]
>>> a.reverse()
>>> a
[1, 2, 3, 4, 5]
>>> a=[1,2,3,4]
>>> a.sort()
>>> a
[1, 2, 3, 4]
>>> a.sort(reverse=True)
>>> a
[4, 3, 2, 1]
>>> def func(a):
... return len(a)
...
>>> def func2(a):
... return a[0]
...
>>> a=["hello","world","ha","hehehehe","ok"]
>>> a.sort(key=func)
>>> a
['ha', 'ok', 'hello', 'world', 'hehehehe']
>>> a.sort(key=func2)
>>> a
['hello', 'ha', 'hehehehe', 'ok', 'world']
>>> a=[1,2,3]
>>> b=["hello","world"]
>>> a
[1, 2, 3]
>>> b
['hello', 'world']
>>> a.extend(b)
>>> a
[1, 2, 3, 'hello', 'world']
>>> b
['hello', 'world']
>>> b.extend(a)
>>> b
['hello', 'world', 1, 2, 3, 'hello', 'world']
>>> a
[1, 2, 3, 'hello', 'world']
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。