标签:分片 索引 rac mat one 相加 out append none
列表是最常用的Python数据类型
>>> l = [1, 2, 3, 4, 5 ]
>>> l
[1, 2, 3, 4, 5]
>>> l = []
>>> l
[]
>>> l = list()
>>> l
[]
>>> l = [‘Apple‘, 123, True]
>>> l
[‘Apple‘, 123, True]
>>> l = list(range(12, 19))
>>> l
[12, 13, 14, 15, 16, 17, 18]
>>> l = [‘physics‘, ‘chemistry‘, 1997, 2000]
>>> l[0]
‘physics‘
>>> l = [1, 2, 3, 4, 5, 6, 7 ]
>>> l[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> l = [1, 2, 3, 4, 5, 6, 7]
>>> l[0]
1
>>> l[-1]
7
list是一个可变的有序表,所以,可以往list中追加元素:
>>> l = [1,2, 3, 4, 5 ]
>>> l.append(6)
>>> l
[1, 2, 3, 4, 5, 6]
>>> l = [1,2, 3, 4, 5 ]
>>> l.insert(1, ‘Jack‘)
>>> l
[1, ‘Jack‘, 2, 3, 4, 5]
>>> l = [‘physics‘, ‘chemistry‘, 1997, 2000]
>>> l
[‘physics‘, ‘chemistry‘, 1997, 2000]
>>> del l[2]
>>> l
[‘physics‘, ‘chemistry‘, 2000]
>>> l = [1, 2, 3, 4, 5 ]
>>> value = l.pop()
>>> l
[1, 2, 3, 4]
>>> value
5
>>> l = [1, 2, 3, 4, 5]
>>> val = l.pop(2)
>>> l
[1, 2, 4, 5]
>>> val
3
>>> l = [123, ‘xyz‘, ‘zara‘, ‘abc‘, ‘xyz‘]
>>> l.remove(‘xyz‘)
>>> l
[123, ‘zara‘, ‘abc‘, ‘xyz‘]
>>> l = [1, 2, 3, 4, 5]
>>> l
[1, 2, 3, 4, 5]
>>> l.clear()
>>> l
[]
>>> classmates = [‘Michael‘, ‘Bob‘, ‘Tracy‘]
>>> classmates[1] = ‘Sarah‘
>>> classmates
[‘Michael‘, ‘Sarah‘, ‘Tracy‘]
>>> l = [‘python‘, ‘java‘, [‘asp‘, ‘php‘], ‘scheme‘]
>>> len(l)
4
>>> s = [‘asp‘, ‘php‘]
>>> l = [‘phthon‘, ‘java‘, s, ‘scheme‘]
>>> l
[‘phthon‘, ‘java‘, [‘asp‘, ‘php‘], ‘scheme‘]
>>> a = [1,2,3, [10, 20, 30]]
>>> b = a.copy()
>>> a[2] = 77
>>> a[3][2] = 666
>>> a
[1, 2, 77, [10, 20, 666]]
>>> b
[1, 2, 3, [10, 20, 666]]
>>> import copy
>>> a = [1,2,3, [10, 20, 30]]
>>> b = copy.copy(a)
>>> a[2] = 77
>>> a[3][2] = 666
>>> a
[1, 2, 77, [10, 20, 666]]
>>> b
[1, 2, 3, [10, 20, 666]]
>>> import copy
>>> a = [1,2,3, [10, 20, 30]]
>>> b = copy.deepcopy(a)
>>> a[2] = 77
>>> a[3][2] = 666
>>> a
[1, 2, 77, [10, 20, 666]]
>>> b
[1, 2, 3, [10, 20, 30]]
>>> l = [1, 2, 3, 4, 5, 6, 7 ]
>>> l[1:4]
[2, 3, 4]
>>> l = [1, 2, 3, 4, 5, 6, 7 ]
>>> l[:]
[1, 2, 3, 4, 5, 6, 7]
>>> l[:4]
[1, 2, 3, 4]
>>> l[2:]
[3, 4, 5, 6, 7]
>>> l = [1, 2, 3, 4, 5, 6, 7 ]
>>> l[1:6:1]
[2, 3, 4, 5, 6]
>>> l[1:6:2]
[2, 4, 6]
>>> l = [1, 2, 3, 4, 5, 6, 7 ]
>>> l[-2:-4]
[]
>>> l[-4:-2]
[4, 5]
>>> l[-1:-5:-1]
[7, 6, 5, 4]
>>> l[-1:-5:-2]
[7, 5]
>>> l[::-1]
[7, 6, 5, 4, 3, 2, 1]
>>> l = [1,2,3,4,5]
>>> for i in l:
... print(i)
...
1
2
3
4
5
>>> a = [1,2,3,4,5,6]
>>> length = len(a)
>>> indx = 0
>>> while indx < length:
... print(a[indx])
... indx += 1
...
1
2
3
4
5
6
>>> l = [["one", 1, "eins"], ["two", 2,"zwei"], ["three", 3,"drei"] ]
>>> for k,v,w in l:
... print(k, "--", v, "--",w)
...
one -- 1 -- eins
two -- 2 -- zwei
three -- 3 -- drei
>>> a = [‘a‘, ‘b‘, ‘c‘]
>>> b = [i for i in a]
>>> b
[‘a‘, ‘b‘, ‘c‘]
>>> a = [1, 2, 3, 4, 5]
>>> b = [i*10 for i in a]
>>> b
[10, 20, 30, 40, 50]
>>> a = range(1, 51)
>>> b = [i for i in a if i % 2 == 0]
>>> b
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]
>>> a = [i for i in range(1,4)]
>>> a
[1, 2, 3]
>>> b = [i for i in range(100,400) if i % 100 == 0]
>>> b
[100, 200, 300]
>>> c = [ m+n for m in a for n in b]
>>> c
[101, 201, 301, 102, 202, 302, 103, 203, 303]
>>> c = [ m+n for m in a for n in b if m+n < 250]
>>> c
[101, 201, 102, 202, 103, 203]
>>> a = [1,2,3,4,5]
>>> b = [5,6,7,8,9]
>>> d = [‘a‘, ‘b‘, ‘c‘]
>>> c = a + b + d
>>> c
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9, ‘a‘, ‘b‘, ‘c‘]
>>> a = [1,2,3,4,5]
>>> b = a *3
>>> b
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>> a = [1,2,3,4,5]
>>> 3 in a
True
>>> 7 in a
False
list(seq) : 将可hash的数据转化为列表
list.sort(cmp = None, key = None, reverse = False) : 对原列表进行排序
>>> str1 = "hi hello world"
>>> str1.split(" ")
[‘hi‘, ‘hello‘, ‘world‘]
>>> str1 = "hi hello world"
>>> list(str1)
[‘h‘, ‘i‘, ‘ ‘, ‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘w‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘]
>>> l = ["hi","hello","world"]
>>> " ".join(l)
‘hi hello world‘
标签:分片 索引 rac mat one 相加 out append none
原文地址:https://www.cnblogs.com/forrest-lin/p/9508320.html