[英]How can I generate a random value and then use the pop method to remove it?
I am trying to take a random name from a list and then once it has been printed, I want to remove it from that list so that it isn't used ever again.我正在尝试从列表中获取一个随机名称,然后一旦打印出来,我想将其从该列表中删除,以便不再使用它。 I want to use the pop method but I'm not sure how to take a random name from the list since the pop method (to my knowledge) only accepts integers.我想使用 pop 方法,但我不确定如何从列表中获取随机名称,因为 pop 方法(据我所知)只接受整数。
Here is my code:这是我的代码:
Boy = ['David','John','Paul','Mark','James','Andrew','Scott','Steven','Robert','Stephen','William','Craig','Michael'
,'Stuart','Christopher','Alan','Colin','Kevin','Gary','Richard','Derek','Martin','Thomas','Neil','Barry',
'Ian','Jason','Iain','Gordon','Alexander','Graeme','Peter','Darren','Graham','George','Kenneth','Allan',
'Simon','Douglas','Keith','Lee','Anthony','Grant','Ross','Jonathan','Gavin','Nicholas','Joseph','Stewart',
'Daniel','Edward','Matthew','Donald','Fraser','Garry','Malcolm','Charles','Duncan','Alistair','Raymond',
'Philip','Ronald','Ewan','Ryan','Francis','Bruce','Patrick','Alastair','Bryan','Marc','Jamie','Hugh','Euan',
'Gerard','Sean','Wayne','Adam','Calum','Alasdair','Robin','Greig','Angus','Russell','Cameron','Roderick',
'Norman','Murray','Gareth','DeanEric','Adrian','Gregor','Samuel','Gerald','Henry','Benjamin','Shaun','Callum',
'Campbell','Frank','Roy','Timothy','Liam','Noah','Oliver','William','Elijah','James','Benjamin','Lucas',
'Mason','Ethan','Alexander','Henry','Jacob','Michael','Daniel','Logan','Jackson','Sebastian','Jack','Aiden',
'Owen','Samuel','Matthew','Joseph','Levi','Mateo','Wyatt','Carter','Julian','Luke','Grayson','Isaac','Jayden'
,'Theodore','Gabriel','Anthony','Dylan','Leo','Christopher','Josiah','Andrew','Thomas','Joshua','Ezra',
'Hudson','Charles','Caleb','Isaiah','Ryan','Nathan','Adrian','Christian']
Girl = ['Emma','Ava','Sophia','Isabella','Charlotte','Amelia','Mia','Harper','Evelyn','Abigail','Emily','Ella',
'Elizabeth','Camila','Luna','Sofia','Avery','Mila','Aria','Scarlett','Penelope','Layla','Chloe','Victoria',
'Madison','Eleanor','Grace','Nora','Riley','Zoey','Hannah','Hazel','Lily','Ellie','Violet','Lillian','Zoe',
'Stella','Aurora','Natalie','Emilia','Everly','Leah','Aubrey','Willow','Addison','Lucy','Audrey','Bella',
'Nova','Brooklyn','Paisley','Savannah','Claire','Skylar','Isla','Genesis','Naomi','Elena','Caroline','Eliana'
,'Anna','Maya','Valentina','Ruby','Kennedy','Ivy','Ariana','Aaliyah','Cora','Madelyn','Alice','Kinsley',
'Hailey','Gabriella','Allison','Gianna,Sarah','Autumn','Quinn','Eva','Piper','Sophie','Sadie','Delilah'
,'Josephine','Nevaeh','Adeline','Arya','Emery','Lydia','Clara','Vivian','Madeline','Peyton','Julia','Rylee',
'Brielle','Reagan','Natalia','Jade'',Athena','Maria','Leilani','Everleigh','Liliana','Melanie','Mackenzie',
'Hadley','Raelynn','Kaylee','Rose','Arianna','Isabelle','Melody','Eliza','Lyla','Katherine','Aubree',
'Adalynn','Kylie','Faith','Marly','Margaret','Ximena','Iris','Alexandra','Jasmine','Charlie','Amaya',
'Taylor','Isabel','Ashley','Khloe','Ryleigh','Alexa','Amara','Valeria','Andrea','Parker','Norah','Eden',
'Elliana','Brianna','Emersyn','Valerie','Anastasia','Eloise','Emerson','Cecilia','Remi','Josie','Reese',
'Bailey','Lucia','Adalyn','Molly','Ayla','Sara','Daisy','London','Jordyn','Esther','Genevieve','Harmony',
'Annabelle','Alyssa','Ariel','Aliyah','Londyn','Juliana','Morgan','Summer','Juliette','Trinity','Callie',
'Sienna','Blakely','Alaia','Kayla','Teagan','Alaina','Brynlee','Finley','Catalina','Sloane','Rachel','Lilly'
,'Ember']
def boyname():
result = Boy.pop(insert code for random name here)
print(result)
print(Boy)
boynames = boyname()
print(boynames)
Simply:简单地:
name = Boy.pop(random.randint(0, len(Boy)-1))
But: you should not do that , especially from within a function, as it affects the caller's list (or in your case, the global variable containing the list).但是:您不应该这样做,尤其是在 function 中,因为它会影响调用者的列表(或者在您的情况下,会影响包含列表的全局变量)。
Consider instead the ability to take a random subset (without side effects):相反,考虑采用随机子集的能力(没有副作用):
sample = random.sample(Boy, k)
For example, you can go through a random ordering of your list (ie sample without replacement), ensuring you will "print" each value just once:例如,您可以 go 通过列表的随机排序(即无需替换的示例),确保您将“打印”每个值一次:
for name in random.sample(Boy, len(Boy)):
...
You can even turn this into a generator, for infinite fun:你甚至可以把它变成一个生成器,以获得无限乐趣:
from itertools import count
def pick(a, n_loops=None):
"""if n_loops is None: cycle endlessly"""
counter = count()
while n_loops is None or next(counter) < n_loops:
yield from random.sample(a, len(a))
Usage:用法:
name_gen = pick(Boy) # infinite generator: loops around shuffles;
# no repeat during a single loop
list(islice(name_gen, 5))
# or
names = pick(Boy, 1) # one random shuffle of Boy
Another fun example:另一个有趣的例子:
for b, g in islice(zip(pick(Boy), pick(Girl)), 4):
print(f'{b} and {g}')
# out:
Carter and Molly
Malcolm and Lydia
Sebastian and Ruby
Charles and Aliyah
Try this code试试这个代码
from random import choice
def boyname():
global Boy
result = choice(Boys)
print(result)
Boy.remove(result)
print(Boy)
boyname()
Something like this would work:像这样的东西会起作用:
import numpy as np
np.random.seed(1)
def boyname():
"""
Returns a random `boy` from `Boy` (list) while removing from list
"""
idx = np.random.randint(0,len(Boy)-1)
res = Boy.pop(idx)
return res
my_boy = boyname()
print(my_boy)
Can use this可以用这个
import random
def boyname():
result = Boy.pop(Boy.index(random.choice(Boy)))
print(result)
print(Boy)
return 'End of Program'
boynames = boyname()
print(boynames)
In my opinion, you can try to get the index of a name by random
function, and you can define the function like this:在我看来,您可以尝试通过random
function 来获取名称的索引,您可以像这样定义 function:
import random
def boyname():
boy_name_list_length = len(Boy)
index_of_name = int(random.random() * boy_name_list_length)
result = Boy.pop(index_of_name)
print(result)
print(Boy)
And call the function like this:并像这样调用 function:
boyname()
Remember just run the Boy
and Girl
list initialization once!记住只运行一次Boy
和Girl
列表初始化!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.