# 洗牌对象列表

import random class a: foo = "bar" a1 = a() a2 = a() b = [a1,a2] print random.shuffle(b)

### 18 Solutions collect form web for “洗牌对象列表”

random.shuffle应该工作。 下面是一个例子，其中的对象是列表：

from random import shuffle x = [[i] for i in range(10)] shuffle(x) # print x gives [[9], [2], [7], [0], [4], [5], [3], [1], [8], [6]] # of course your results will vary

import random a = range(5) b = random.sample(a, len(a)) print a, b, "two list same:", a == b # print: [0, 1, 2, 3, 4] [2, 1, 3, 4, 0] two list same: False # The function sample allows no duplicates. # Result can be smaller but not larger than the input. a = range(555) b = random.sample(a, len(a)) print "no duplicates:", a == list(set(b)) try: random.sample(a, len(a) + 1) except ValueError as e: print "Nope!", e # print: no duplicates: True # print: Nope! sample larger than population
#!/usr/bin/python3 import random s=list(range(5)) random.shuffle(s) # << shuffle before print or assignment print(s) # print: [2, 4, 1, 3, 0]

>>> import random >>> a = ['hi','world','cat','dog'] >>> random.shuffle(a,random.random) >>> a ['hi', 'cat', 'dog', 'world']

import numpy as np np.random.shuffle(b) print(b)

'print func（foo）'将在用'foo'调用时打印'func'的返回值。 'shuffle'然而没有None作为它的返回types，因为这个列表将被修改，因此它不会打印任何东西。 解决方法：

# shuffle the list in place random.shuffle(b) # print it print(b)

def myshuffle(ls): random.shuffle(ls) return ls
from random import random my_list = range(10) shuffled_list = sorted(my_list, key=lambda x: random())

def shuffled(x): import random y = x[:] random.shuffle(y) return y x = shuffled([1, 2, 3, 4]) print x

numpy.random.shuffle

>>> import numpy as np >>> import random

>>> foo = np.array([[1,2,3],[4,5,6],[7,8,9]]) >>> foo array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> random.shuffle(foo) >>> foo array([[1, 2, 3], [1, 2, 3], [4, 5, 6]])

>>> foo = np.array([[1,2,3],[4,5,6],[7,8,9]]) >>> foo array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.random.shuffle(foo) >>> foo array([[1, 2, 3], [7, 8, 9], [4, 5, 6]])

import random perm = list(range(len(list_one))) random.shuffle(perm) list_one = [list_one[index] for index in perm] list_two = [list_two[index] for index in perm]

## Numpy / Scipy

import numpy as np perm = np.random.permutation(len(list_one)) list_one = list_one[perm] list_two = list_two[perm]
def shuffle(_list): if not _list == []: import random list2 = [] while _list != []: card = random.choice(_list) _list.remove(card) list2.append(card) while list2 != []: card1 = list2[0] list2.remove(card1) _list.append(card1) return _list

>>> A = ['r','a','n','d','o','m'] >>> B = [1,2,3,4,5,6] >>> import random >>> random.sample(A+B, len(A+B)) [3, 'r', 4, 'n', 6, 5, 'm', 2, 1, 'a', 'o', 'd']

from random import * def listshuffler(inputlist): for i in range(len(inputlist)): swap = randint(0,len(inputlist)-1) temp = inputlist[swap] inputlist[swap] = inputlist[i] inputlist[i] = temp return inputlist

ml = [[0], [1]] * 10

random.shuffle(ml)

[0]的数目可以是9或8，但不完全是10。

import random iteration = random.randint(2, 100) temp_var = 0 while iteration > 0: for i in range(1, len(my_list)): # have to use range with len() for j in range(1, len(my_list) - i): # Using temp_var as my place holder so I don't lose values temp_var = my_list[i] my_list[i] = my_list[j] my_list[j] = temp_var iteration -= 1

from random import shuffle def foo1(): print "foo1", def foo2(): print "foo2", def foo3(): print "foo3", A=[foo1,foo2,foo3] for x in A: x() print "\r" shuffle(A) for y in A: y()

• 截断一个string，而不是在一个字的中间结束
• 在OpenCV / Python中设置摄像机参数
• 像C＃中的StringBuilder的Pythonstring类？
• Python的“超级”如何做正确的事情？
• 我们如何使用SQL-esque“LIKE”标准来join两个Spark SQL数据框？
• 如何从基于条件expression式的pandas DataFrame中删除行
• Python __str__和列表
• 最好的方法来生成XML？
• 查找string中子序列的出现次数
• Python中的二进制数字
• Pythonic的方式来打印列表项目