python中的列表与列表推导式

>>> x = list(range(10))                         #创建列表
>>> print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> import random
>>> random.shuffle(x)                        #把列表中的元素打乱顺序
>>> x
[4, 0, 2, 7, 3, 9, 1, 6, 8, 5]

>>> x[2]
2
>>> x[-1]                                            #访问最后一个元素
5
>>> x[-2]
8

猜你喜欢

转载自www.cnblogs.com/zhn620/p/9218411.html