python 列表常用的方法
1.append( ):用于在列表末尾添加新的对象
list.appent(obj) #obj:添加到列表末尾的对象
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.append(2009)
print("Updated List:",aList) #输出结果:Updated List: [123, 'xyz', 'zara', 'abc', 2009]
extend( ):将列表元素(或任何可迭代的元素)添加到当前列表的末尾
list.extend(obj) #obj:列表元素或可迭代的元素
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.extend([2009])
print(aList) #输出结果:[123, 'xyz', 'zara', 'abc', 2009]
2. clear( ):删除列表中的所有元素
list.clear( )
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.clear()
print(aList) #输出结果:[]
pop( ):删除指定位置的元素,被删除的值可以继续访问。
list.pop(position) #position:指定的下标
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.pop(1)
print(aList) #输出结果:[123, 'zara', 'abc']
#列表中的摩托车是按购买时间存储的,就可使用方法pop()打印一条消息,指定最后购买的是哪款摩托车
motorcycles = ['honda','yamaha','suzuki']
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")
#输出结果:The last motorcycle I owned was a Suzuki.
remove( ):删除具有指定值的项目,当不知道该值所处的位置时。remove只删除第一个指定的值,如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有的值。
list.remove(value) #value:指定的值
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.remove(’xyz')
print(aList) #输出结果:[123, 'zara', 'abc']
del语句:删除元素,需要知道要删除的元素在列表中的位置。使用del语句将值从列表中删除后,就无法再访问它了。
name = ["crystal","Lily","Lucy","James"]
del name[0]
print(name)
#输出结果:['Lily', 'Lucy', 'James']
3. copy( ):返回列表的副本
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
bList = aList.copy()
print(aList)
print(bList)
#输出结果:[123, 'xyz', 'zara', 'abc']
[123, 'xyz', 'zara', 'abc']
4.count( ):返回具有指定值得元素数量
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
print(aList.count(123)) #输出结果:1
5. insert( ):在指定位置添加元素,需要指定新元素的索引和值
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.insert(1,'crystal')
print(aList) #输出结果:[123, 'crystal', 'xyz', 'zara', 'abc']
6. index( ):返回具有指定值的第一个元素的索引
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
print(aList.index("xyz")) #查找位置,输出结果:1
print(aList[aList.index("xyz")]) #查找位置并打印出来,输出结果:xyz
7. reverse( ):,倒着打印列表,颠倒列表的顺序。reverse永久性修改列表元素的排序,但可以随时恢复到原来的排序,为此只需要对列表再次调用reverse方法即可。
#!/usr/bin/python
aList = [123,'xyz','zara','abc']
aList.reverse()
print(aList) #输出结果:['abc', 'zara', 'xyz', 123]
sort( ):对列表进行永久性排序
#!/usr/bin/python
aList = ['123','xyz','zara','abc']
aList.sort()
print(aList) #输出结果:['123', 'abc', 'xyz', 'zara'],备注:python3中是不能把不同的数据类型进行sort的,会报错
#按照与字母顺序相反的顺序进行排序
cars=['bmw','audi','toyota']
cars.sort(reverse=True)
print(cars)
#输出结果:['toyota', 'bmw', 'audi']
sorted( ):对列表临时排序,同时不影响它们在列表中的原始排序
cars=['bmw','audi','toyota']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the reverse list:")
print(sorted(cars,reverse = True))
print("\nHere is the original list again:")
print(cars)
#输出结果:
Here is the original list:
['bmw', 'audi', 'toyota']
Here is the sorted list:
['audi', 'bmw', 'toyota']
Here is the reverse list:
['toyota','bmw','audi']
Here is the original list again:
['bmw', 'audi', 'toyota']
8. 拼接
name = ["crystal","Lily","Lucy","James"]
message = "The beatiful girl is : "+name[0]
print(message)
#输出结果:The beatiful girl is : crystal
9. 修改列表元素:指定列表名和修改的元素的索引,再指定该元素的新值
name = ["crystal","Lily","Lucy","James"]
name[0]="Mary"
print(name)
#输出结果:['Mary', 'Lily', 'Lucy', 'James']
10. len( ):确定列表长度。python算列表元素数是从1开始。
cars=['bmw','audi','toyota']
print(len(cars))
#输出结果:3
11. 使用列表时避免索引错误
cars=['bmw','audi','toyota']
print(cars[3])
#输出结果:IndexError: list index out of range