python中列表(list)的基本操作

列表

创建和访问列表

bicycle = ['trek', 'cannondale', 'redline','specialized']
print bicycle
print bicycle[0] # 下标从0开始
print bicycle[-1] # 最后一个元素
print bicycle[:1]
print bicycle[1:3]
print bicycle[0].title() # 首字母大写

message = 'My first bicycle was a '+bicycle[0].title()+'.'
print message
['trek', 'cannondale', 'redline', 'specialized']
trek
specialized
['trek']
['cannondale', 'redline']
Trek
My first bicycle was a Trek.

修改、添加和删除元素

## 修改指定元素
bicycle = ['trek', 'cannondale', 'redline','specialized']

bicycle[1] = 'baoma'
bicycle[2:] = ['baoma','haima']
print bicycle
['trek', 'baoma', 'baoma', 'haima']
## 添加元素
bicycle.append('xiaodao') # 末尾添加
print bicycle

bicycle.insert(1,'bb') # 指定位置插入
print bicycle
['trek', 'baoma', 'baoma', 'haima', 'xiaodao']
['trek', 'bb', 'baoma', 'baoma', 'haima', 'xiaodao']
## 删除元素
bicycle = ['trek0', 'trek1','trek2','trek3','trek4','trek5']

del bicycle[1]
print bicycle

bicycle_pop = bicycle.pop() # 相当于从列表中取出最后一个元素,此时列表中不再有该元素
print bicycle_pop
print bicycle

bicycle_pop0 = bicycle.pop(0) # 取出指定位置的元素,此时列表中不再有该元素
print bicycle_pop0
print bicycle

bicycle.remove('trek3') # 用于不知道具体的索引位置,只知道具体的值
print bicycle
['trek0', 'trek2', 'trek3', 'trek4', 'trek5']
trek5
['trek0', 'trek2', 'trek3', 'trek4']
trek0
['trek2', 'trek3', 'trek4']
['trek2', 'trek4']

列表操作

## sort永久性排序
cars = ['bwm','audi','toyota','subaru']
cars.sort() # 对列表进行永久性排序(改变了列表本身)
print cars

cars = ['bwm','audi','toyota','subaru']
cars.sort(reverse=True) #设置排序时是否倒序
print cars

## sorted临时排序
cars = ['bwm','audi','toyota','subaru']
print sorted(cars) # 列表自身元素未发生改变
print cars
['audi', 'bwm', 'subaru', 'toyota']
['toyota', 'subaru', 'bwm', 'audi']
['audi', 'bwm', 'subaru', 'toyota']
['bwm', 'audi', 'toyota', 'subaru']
## 倒着打印列表
cars = ['bwm','audi','toyota','subaru']
print cars
cars.reverse() #列表元素顺序被翻转(再调用一次即可恢复原来顺序)
print cars

## 确定列表长度
print len(cars)
['bwm', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bwm']
4
## 创建数值列表
number1 = list(range(1,6))
print number1

number2 = list(range(1,11,2))
print number2
[1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
## 通过循环创建所需列表
squares1 = []
for value in range(1,11,2):
    square = value**2
    squares1.append(square)

print squares1

squares2 = [value**2 for value in range(1,11,2)]
print squares2
[1, 9, 25, 49, 81]
[1, 9, 25, 49, 81]
## 对数字列表进行简单的统计计算
digits = [1,2,3,4,5]
print min(digits)
print max(digits)
print sum(digits)
1
5
15
## 切片
players = ['aaa','bbb','ccc','ddd','eee','fff']
print players[0:3]
print players[:3]
print players[3:]
print players[:-3]
print players[-3:]

## 遍历
players = ['aaa','bbb','ccc','ddd','eee','fff']
for player in players[:3]:
    print player.title()

## 复制(注意两种复制结果的不同)
print '--------------------------------------------'
players = ['aaa','bbb','ccc','ddd','eee','fff']
players_copy1 = players[:]# 产生两个列表(两个列表复制后无关系)
print players_copy1
players.append('ggg1')
players_copy1.append('ggg2')
print players
print players_copy1
print '------------------'
players = ['aaa','bbb','ccc','ddd','eee','fff']
players_copy2 = players # 产生同一个列表的副本,变动一个列表,另一个也会变动
print players_copy2
players.append('ggg1')
players_copy2.append('ggg2')
print players
print players_copy2
['aaa', 'bbb', 'ccc']
['aaa', 'bbb', 'ccc']
['ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc']
['ddd', 'eee', 'fff']
Aaa
Bbb
Ccc
--------------------------------------------
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg2']
------------------
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1', 'ggg2']
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg1', 'ggg2']

元祖(不可变的列表)

## 定义元祖
dims = (20,50)
print dims[0],dims[1]
print type(dims)

##元祖不可改
dims[1] = 30 # 此处会报错
20 50




---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

 in ()
      5 
      6 ##元祖不可改
----> 7 dims[1] = 30 # 此处会报错


TypeError: 'tuple' object does not support item assignment
## 遍历元祖
dims = (10,20,30,40)
for dim in dims:
    print dim
10
20
30
40
## 元祖赋值(注意不同于元祖修改)
dims = (10,20,30)
print dims

dims = (40,50,60) # 此时整个元祖的所有元素被新的元素覆盖
print dims
(10, 20, 30)
(40, 50, 60)

你可能感兴趣的:(Python)