In [120]: import randomIn [121]: l1 = (1, 2, 3)In [122]: l2 = [1, 2, 3]In [123]: ranrandom range In [123]: random.shuffle(l1)---------------------------------------------------------------------------TypeError Traceback (most recent call last)in ()----> 1 random.shuffle(l1)/usr/lib/python2.7/random.pyc in shuffle(self, x, random) 287 # pick an element in x[:i+1] with which to exchange x[i] 288 j = _int(random() * (i+1))--> 289 x[i], x[j] = x[j], x[i] 290 291 def sample(self, population, k):TypeError: 'tuple' object does not support item assignmentIn [124]: random.shuffle(l2) In [125]: l2Out[125]: [3, 2, 1]
In [126]: a = l2In [127]: aOut[127]: [3, 2, 1]In [128]: a = random.shuffle(l2)In [129]: aIn [130]: aIn [131]: print aNone
上面的错误是因为元组内容不可更改
下面返回a的值是None是因为shuffle返回值的原因 ,shuffle只是在原基础上做的打乱
In [19]: import randomIn [20]: random.random()Out[20]: 0.9467622073347552In [21]: random.uniform(1, 2)Out[21]: 1.5264479206993298In [22]: random.uniform(1, 1)Out[22]: 1.0In [23]: random.randint(1,3)Out[23]: 1In [24]: random.randrange(1, 100)Out[24]: 7In [25]: random.randrange(1, 100, 3)Out[25]: 22In [26]: random.randrange(1, 100, 3)Out[26]: 28In [27]: random.randrange(1, 100, 3)Out[27]: 58In [28]: random.randrange(1, 100, 3)Out[28]: 91In [29]: random.choice(range(10))Out[29]: 5In [30]: random.sample(range(10), 3)Out[30]: [9, 6, 2]