python 列表

list('hello')
['h', 'e', 'l', 'l', 'o']

 

操作方法:

1.改变例表:元素赋值

x = [1, 1, 1]
x [1] = 2
x
[1, 2, 1]

2. 删除元素

names = ['tu','dong', 'nana', 'test']
names
['tu', 'dong', 'nana', 'test']
del names[3]
names
['tu', 'dong', 'nana']

 

3. 分片赋值

name = list('perl')
name
['p', 'e', 'r', 'l']
name[2:] = list('ar')

 

表列方法:append

lst = [1, 2, 3]
lst.append(1)
lst
[1, 2, 3, 1]

列表方法:count

['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2

列表方法: extend

>>> a = [1,2,3]
>>> b = [3,4,5]
>>> a.extend(b)
>>> a
[1, 2, 3, 3, 4, 5]

列表方法:index

>>> words = ['I', 'am', 'pan', 'zi', 'dong']
>>> words.index('pan')
2

 

列表方法:insert

>>> numbers = [1,2,3,4,5,6,7]
>>> numbers.insert(4,'test')
>>> numbers
[1, 2, 3, 4, 'test', 5, 6, 7]

 

列表方法:pop

>>> x = [1,2,3]
>>> x.pop()
3
>>> x
[1, 2]
>>> x.pop(0)
1

 

列表方法: remove

>>> x = ['to','be','or']
>>> x.remove('be')
>>> x
['to', 'or']

 

列表方法: reverse

>>> x
['to', 'or']
>>> x.reverse()
>>> x
['or', 'to']

 

列表方法: sort

>>> x = [4,6,7,3,1]
>>> x.sort()
>>> x
[1, 3, 4, 6, 7]

 

列表方法:cmp

 

now not support

posted @ 2019-06-24 15:08  嵌入式实操  阅读(142)  评论(0编辑  收藏  举报