How to randomly select an item from a list?
假设我有以下列表:
1 | foo = ['a', 'b', 'c', 'd', 'e'] |
从列表中随机检索项目的最简单方法是什么?
使用
1 2 3 4 | import random foo = ['a', 'b', 'c', 'd', 'e'] print(random.choice(foo)) |
对于加密安全的随机选择(例如,用于从单词表生成密码短语),请使用
1 2 3 4 5 | import random foo = ['battery', 'correct', 'horse', 'staple'] secure_random = random.SystemRandom() print(secure_random.choice(foo)) |
或
1 2 3 | import secrets foo = ['a', 'b', 'c', 'd', 'e'] print(secrets.choice(foo)) |
如果您想从列表中随机选择多个项目,或者从集合中选择一个项目,我建议您使用
1 2 3 4 5 6 | import random group_of_items = {1, 2, 3, 4} # a sequence or set will work here. num_to_select = 2 # set the number to select here. list_of_random_items = random.sample(group_of_items, num_to_select) first_random_item = list_of_random_items[0] second_random_item = list_of_random_items[1] |
但是,如果您只从列表中提取一个项目,那么选择就不那么繁琐了,因为使用sample的语法应该是
不幸的是,choice只适用于序列(如列表或元组)的单个输出。虽然
编辑:使用机密
正如许多人所指出的,如果您需要更安全的伪随机样本,则应使用机密模块:
1 2 3 4 5 6 7 | import secrets # imports secure module. secure_random = secrets.SystemRandom() # creates a secure random object. group_of_items = {1, 2, 3, 4} # a sequence or set will work here. num_to_select = 2 # set the number to select here. list_of_random_items = secure_random.sample(group_of_items, num_to_select) first_random_item = list_of_random_items[0] second_random_item = list_of_random_items[1] |
如果您还需要索引,请使用
1 2 3 | from random import randrange random_index = randrange(len(foo)) print(foo[random_index]) |
从python 3.6开始,您可以使用
要从列表中打印随机元素:
1 2 3 | import secrets foo = ['a', 'b', 'c', 'd', 'e'] print(secrets.choice(foo)) |
打印随机索引:
1 | print(secrets.randbelow(len(foo))) |
有关详细信息,请参阅PEP 506。
我建议使用一个脚本来删除列表中随机选取的项目,直到它为空:
维护一个
1 2 3 4 5 6 | s=set(range(1,6)) import random while len(s)>0: s.remove(random.choice(list(s))) print(s) |
三次跑步给出三个不同的答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | >>> set([1, 3, 4, 5]) set([3, 4, 5]) set([3, 4]) set([4]) set([]) >>> set([1, 2, 3, 5]) set([2, 3, 5]) set([2, 3]) set([2]) set([]) >>> set([1, 2, 3, 5]) set([1, 2, 3]) set([1, 2]) set([1]) set([]) |
1 2 | foo = ['a', 'b', 'c', 'd', 'e'] number_of_samples = 1 |
在Python 2中:
1 | random_items = random.sample(population=foo, k=number_of_samples) |
在Python 3中:
1 | random_items = random.choices(population=foo, k=number_of_samples) |
如果需要索引,只需使用:
1 2 3 4 | import random foo = ['a', 'b', 'c', 'd', 'e'] print int(random.random() * len(foo)) print foo[int(random.random() * len(foo))] |
random.choice也一样:)
对于这个问题,它的工作原理与公认的答案(
1 2 | import numpy as np np.random.choice(foo) # randomly selects a single item |
对于再现性,您可以执行以下操作:
1 2 | np.random.seed(123) np.random.choice(foo) # first call will always return 'c' |
对于作为
1 2 | np.random.choice(foo, 5) # sample with replacement (default) np.random.choice(foo, 5, False) # sample without replacement |
How to randomly select an item from a list?
Assume I have the following list:
1 foo = ['a', 'b', 'c', 'd', 'e']What is the simplest way to retrieve an item at random from this list?
如果您希望接近真正的随机,那么我建议使用来自
1 2 3 4 5 | >>> import random >>> sr = random.SystemRandom() >>> foo = list('abcde') >>> foo ['a', 'b', 'c', 'd', 'e'] |
现在:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> sr.choice(foo) 'd' >>> sr.choice(foo) 'e' >>> sr.choice(foo) 'a' >>> sr.choice(foo) 'b' >>> sr.choice(foo) 'a' >>> sr.choice(foo) 'c' >>> sr.choice(foo) 'c' |
如果要进行确定性伪随机选择,请使用
1 2 | >>> random.choice <bound method Random.choice of <random.Random object at 0x800c1034>> |
它看起来是随机的,但实际上不是,我们可以看到,如果我们重复地重新设置它:
1 2 3 4 5 6 7 8 9 10 | >>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo) ('d', 'a', 'b') >>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo) ('d', 'a', 'b') >>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo) ('d', 'a', 'b') >>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo) ('d', 'a', 'b') >>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo) ('d', 'a', 'b') |
这是带有定义随机索引的变量的代码:
1 2 3 4 5 6 | import random foo = ['a', 'b', 'c', 'd', 'e'] randomindex = random.randint(0,len(foo)-1) print (foo[randomindex]) ## print (randomindex) |
这是不带变量的代码:
1 2 3 4 | import random foo = ['a', 'b', 'c', 'd', 'e'] print (foo[random.randint(0,len(foo)-1)]) |
这是最短和最聪明的代码:
1 2 3 4 | import random foo = ['a', 'b', 'c', 'd', 'e'] print(random.choice(foo)) |
(Python 2.7)
如果您正在寻找这样的解决方案:
1 2 3 4 5 6 | from random import * library = ["New York","Berlin","Rome"] for x in range (10): i = randrange(0,3) print(library[i]) |
下面的代码演示您是否需要生成相同的项。还可以指定要提取的样本数。
1 2 3 4 5 | import random as random random.seed(0) # don't use seed function, if you want different results in each run print(random.sample(foo,3)) # 3 is the number of sample you want to retrieve Output:['d', 'e', 'a'] |
我们也可以使用randint。
1 2 3 4 5 6 7 8 9 10 | from random import randint l= ['a','b','c'] def get_rand_element(l): if l: return l[randint(0,len(l)-1)] else: return None get_rand_element(l) |
我这样做是为了让它工作:
1 2 3 | import random pick = ['Random','Random1','Random2','Random3'] print (pick[int(random.random() * len(pick))]) |