Python 列表基础
1.列表的表现形式
[1,2,3,4,5]
['a','b','c','d']
2.用下标取得单个值
>spam = ['a','b','c','d']
>spam[0]
'a'
注意:下标为整数,否则出现报错,
>spam = [['a','b','c','d'],[1,2,3,4,5]]
>spam[1][4]
>5
3.负数下标
>spam = ['a','b','c','d']
>spam[-1]
'd'
>spam[-2]
'c'
4.利用切片取子列表
>spam = ['a','b','c','d']
>spam[0:4]
'a,','b','c','d'
>spam[0:3]
'a,','b','c'
5.利用len()语句取的列表长度
>spam = ['a','b','c','d']
>len(spam)
4
6.利用下标改变列表中的值
>spam = ['a','b','c','d']
>spam[0] = 'k'
>spam
['k,','b','c','d']
7.列表的连接和复制
>['a','b','c','d'] + [1,2,3]
['a','b','c','d',1,2,3]
>spam = ['a','b','c','d']
>spam = spam +[2,3,4]
['a','b','c','d',2,3,4]
>spam = [1,2,3]
>spam *3
[1,2,3,1,2,3,1,2,3]
8.del语句删除列表中的值
>spam = ['a','b','c','d']
>del spam[0]
>spam
['b','c','d']
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。