python列表
一、列表的创建与操作
1.创建列表
a = [] ##元素类型可为int,float,complex,str,list,tuple
b = [a, 1, True, 3j + 2, "hi"]
c = [[1, 2, 3, 4], [a, b], 233, "hello"]
d = [a, b, c ]
2.列表的索引与切片
>> c = [[1, 2, 3, 4], [5,6], 233, "hello"]">>>> c
[[1, 2, 3, 4], [5, 6], 233, 'hello']
>>> type(c) ##显示类型
<class 'list'>
>>> c[0][-1]
4
>>> c[-1][3]
'l'
>>> c[-2]
233
3.强制转换
>>> s=list(range(5)) ##采用内置方法list()强制转换
>>> s
[0, 1, 2, 3, 4]
>>> s[3]
3
4.列表的重复
>>> list=['a',3,True,'hello']
>>> list*3
['a', 3, True, 'hello', 'a', 3, True, 'hello', 'a', 3, True, 'hello']
5.列表的成员操作符
>>> list
['a', 3, True, 'hello']
>>> 'a' in list ##判断元素是否在列表中
True
>>> 'test' in list
False
>>> True in list
True