列表(list)的操作

列表的特点:

1、列表中可以存在各种类型的数据

>>> [1,2,'abc',[2,4,5],True,False]
[1, 2, 'abc', [2, 4, 5], True, False]

2、列表截取时,如果使用序列的方式提取得到的是字符串,如果使用’:‘的方式提取得到还是个列表 

>>> ['abc','python','c++','java'][0]
'abc'                                 使用序列的方式提取得到的是字符串
>>> ['abc','python','c++','java'][2]
'c++'
>>> ['abc','python','c++','java'][0:2]
['abc', 'python'] 
>>> ['abc','python','c++','java'][-1:]
['java']                             ‘:’的方式提取得到还是个列表 

注:列表截取与字符串截取方式一样

列表的操作:

>>> ['abc','python'] + ['c++','java']                  加法 +
['abc', 'python', 'c++', 'java']
>>> ['abc','python']*3                                 乘法 *
['abc', 'python', 'abc', 'python', 'abc', 'python']
>>> ['abc','python'] - ['python']                      不支持 减法 -
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    ['abc','python'] - ['python']
TypeError: unsupported operand type(s) for -: 'list' and 'list'


猜你喜欢

转载自blog.csdn.net/weixin_41355124/article/details/80312382
今日推荐