假设我有十个城市的列表:
["paris", "barcelona", "kolkata", "new york"]
如何模拟两个属性的n个随机数据集,每个属性包含从上面列表中选择的2个城市:
["paris", "barcelona"]
["barcelona", "kolata"]
["new york", "paris"]
["paris", "barcelona"]
["new york", "paris"]
答案 0 :(得分:4)
您可以像这样使用random.sample
data = ["paris", "barcelona", "kolkata", "new york"]
import random
print [random.sample(data, 2) for _ in xrange(5)]