(二)列表结构
2.1 列表的定义
List里可以包含任何东西,其中元素之间可以没有任何关系。
在Python中,用[]来表示列表,并用逗号来分隔其中的元素。
List没有长度限制。
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
2.2 访问列表元素
squares = [1,4,9,16,25]
print(squares)
print(squares[0])
print(squares[-1])
print(squares[-3:])
print(squares[:])
print(squares + [36,49,64,81,100])
[1, 4, 9, 16, 25]
1
25
[9, 16, 25]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Python访问最后一个元素提供了一种特殊语法。通过将索引指定为-1,可让Python返回最后一个列表的元素。
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[-1])
specialized
例如,索引-2返回倒数第二个列表元素,索引-3返回倒数第三个列表元素,以此类推。2.3 修改列表元素
cubes = [1,8,27,65,125]
print(cubes)
a = (4 ** 3)
cubes[3] = a
print(cubes)
[1, 8, 27, 65, 125]
[1, 8, 27, 64, 125]
你还可以在列表的末尾添加新的项目,利用append()方法:
cubes.append(216)
cubes.append(7 ** 3)
print(cubes)
[1, 8, 27, 64, 125, 216, 343]
对切片的赋值也是可能的,这甚至可以改变列表的大小或者完全清除它:
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(letters)
# replace some values
letters[2:5] = ['C', 'D', 'E']
print(letters)
# now remove them
letters[2:5] = []
print(letters)
# clear the list by replacing all the elements with an empty list
letters[:] = []
print(letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'C', 'D', 'E', 'f', 'g']
['a', 'b', 'f', 'g']
[]
2.4 在列表里添加元素
使用insert()可以在列表的任何位置添加新元素。为此,你需要指定新元素的索引和值。
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.insert(0,'ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'honda', 'yamaha', 'suzuki']
2.5 在列表里删除元素
2.5.1 使用del语句删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
2.5.2 使用pop()方法删除元素
pop()可删除列表末尾的元素,并让你能够直接使用它。
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
poped_motorcycles = motorcycles.pop()
print(motorcycles)
print(poped_motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
2.5.3 根据值删除元素
如果你只知道要删除的元素值,可使用方法remove。
motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
2.6 确定列表长度
使用len()函数获取列表长度。
motorcycles = ['honda','yamaha','suzuki']
print(len(motorcycles))
3
注意:Python 计算列表元素时从1开始。2.7 对列表排序
2.7.1 sort()函数对列表永久性排序
motorcycles = ['honda','yamaha','suzuki','abc']
print(motorcycles)
motorcycles.sort()
print(motorcycles)
motorcycles.sort(reverse=True)
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'abc']
['abc', 'honda', 'suzuki', 'yamaha']
['yamaha', 'suzuki', 'honda', 'abc']
2.7.2 sorted()函数对列表临时排序
motorcycles = ['honda','yamaha','suzuki','abc']
print(motorcycles)
print(sorted(motorcycles))
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'abc']
['abc', 'honda', 'suzuki', 'yamaha']
['honda', 'yamaha', 'suzuki', 'abc']
2.8 简单的应用
例一:
a ,b = 0,1
while b < 10:
print(b)
a,b = b,a+b
1
1
2
3
5
8
例二:
i = 256*256
print('The value of i is ',i)
he value of i is 65536
例三:
a , b=0,1
while b< 1000:
print(b,end = ',')
a ,b = b ,a+b
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,