手工写一个列表:
>>>aList = [123,'abc',4.56,['inner','list'],7-9j]
[123,'abc',4.56,['inner','list'],(7-9j)]
工厂方式创建:
>>>list('foo')
['f','o','o']
访问列表:
切片操作
更新列表:
1、元素追加 append()
>>> aList = ['happy','new',123]
>>> aList.append('year')
>>> aList
['happy','new',123,'year']
2、元素删除 remove()
>>> aList
['happy','new',123,'year']
>>> aList.remove(123)
>>> aList
['happy','new','year']
删除列表
>>> del aList
列表的连接 ‘+’或append()
注意:连接操作符不能向列表中添加新元素,因为连接操作只能在同类型
之间进行。此时我们可以使用append()
例:
>>> aList.extend('new Item')
tuple() ---> 可以将列表转换成元组
>>> aList = ['tao',93,99,'time']
>>> aTuple = tuple(aList)
>>> aTuple
('tao' 93,99,time)
列表类型的内建函数:
range() 接受一个数值作为输入,输出一个符合标准的列表。
此外,我们还可以通过dir() 方法来得到它所有的方法和属性
list Method Operation
list.append(obj) 向列表中添加一个对象obj
list.count(obj) 返回一个对象obj在列表中出现的次数
list.extend(seq) 把序列seq的内容添加到列表中
list.index(obj,i=0,j=len(list))返回obj在list中的索引值
list.insert(index,obj) 在索引量为index的位置插入对象obj
list.remove(obj) 从列表中删除对象obj
list.reverse() 反转
list.sort(func=None,key=None,reverse=False)
以指定的方式排序