1.索引和切片,通过索引和切片,将相应的值取出来,然后替换成新的值
>>> aList=['123',123,'a']
>>> aList[0]=1
>>> aList
[1, 123, 'a']
>>> aList[0:1]=[234]
>>> aList
[234, 123, 'a']
>>>下面举一个利用切片调换值的例子
>>> aList=['123',123,'a']
>>> aList[0:2]=aList[1:3]
>>> aList
[123, 'a', 'a']
>>>
上面方法的执行顺序是:(对于aList[0:2]=aList[1:3])
1)先把aList[1:3]所对应的值取出来
2)删除aList[0:2]的值
3)把aList[1:3]替换到aList[0:2]上
2.使用列表方法替换
1)使用append与+
使用append
>>> aList=['123',123,'a']
>>> aList.append (234)
>>> aList
['123', 123, 'a', 234]
>>>
>>> aList=['abA','ABb','aBC','Abd',123]
>>> aList.append (['123'])
>>> aList
['ABb', 'Abd', 'aBC', 'abA', 123, ['123']]
>>>
使用+
>>> aList=['123',123,'a']
>>> aList+[234]
['123', 123, 'a', 234]
>>>结果是一样的,而且都是原处修改
>>> aList=['123',123,'a']
>>> id(aList)
36649920
>>> aList.append (234)
>>> id(aList)
36649920
>>> aList+['efg']
['123', 123, 'a', 234, 'efg']
>>> id(aList)
36649920
>>>
通过上面的代码可以看见,id始终没有变化,也就是说aList没有创建新的对象,都是使用同一个对象
3.排序
>>> aList=['abA','ABb','aBC','Abd']
>>> aList.sort ()
>>> aList
['ABb', 'Abd', 'aBC', 'abA']
>>> aList.sort (key=str.lower ,reverse=True)
>>> aList
['Abd', 'aBC', 'ABb', 'abA']
>>> aList.sort (key=str.upper ,reverse=True)
>>> aList
['Abd', 'aBC', 'ABb', 'abA']
>>> aList.sort (key=str.upper ,reverse=False)
>>> aList
['abA', 'ABb', 'aBC', 'Abd']
>>>通过改变sort里面的参数,使得aList 的排序方式发生改变
注意:排序是对于同一类型对象的,不同类型的对象不能排序
>>> aList=['abA','ABb','aBC','Abd',123]
>>> aList.sort ()
Traceback (most recent call last):
File "", line 1, in
aList.sort ()
TypeError: unorderable types: int() < str()
>>>
4.其他的一些方法
>>> aList=['abA','ABb','aBC','Abd',123]
>>> aList.pop (4)
123
>>> aList
['abA', 'ABb', 'aBC', 'Abd']
>>> aList.reverse ()
>>> aList
['Abd', 'aBC', 'ABb', 'abA']
>>> aList.index ('aBC')
1
>>> aList.insert (2,'efg')
>>> aList
['Abd', 'aBC', 'efg', 'ABb', 'abA']
>>> del aList[3]
>>> aList
['Abd', 'aBC', 'efg', 'abA']
>>>
就说到这里,谢谢大家
------------------------------------------------------------------