Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with lists
#1
Dear friends,

I simply can't get my head around this little problem. I need to randomly choose three words out of a list of words. The chosen word cannot be a repetition and there has has to be exactly three - it is especially the last part that gets me. What I've done so far is this

import random
nouns = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "extrovert", "gorilla"]

new_lst =  []

select_noun = random.choice(nouns)

while select_noun not in new_lst :
    new_lst.append(select_noun)
    select_noun = random.choice(nouns)
    if len(new_lst) == 3:
        break
    
print (new_lst)   
     
I get three words most of the times but as you can probably see it is open for making one or two-word lists as well.

Can you assist on this?
Reply
#2
The problem with you code is that the while loop depends on the selected noun not being in the list.
It can select an already selected item, as soon as it does it will stop looping.
You need to rethink the logic.
you could remove the selected item from the available selections
You could also make the loop not dependant on whats in the list, only adding to the list if the selected noun is not already in the list, that way it would continue looping re selecting items until it finds 3 unique items.
Reply
#3
Hi

OK - yes,you're right. The thing is that I simply can't figure out how. Now, based on you're reply, I tried to put in the following, which seem more clear. Still doesn't work - or in fact I'm not sure because I don't get any response in my VScode, which could lead to something being absolutely wrong in the code.

import random
nouns = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "gorilla"]

new_lst =  []
i = 0

while i < 3 :
    select_noun = random.choice(nouns)
    if select_noun.index in new_lst == False :
        new_lst.append(select_noun)

i = len(new_lst)

print (new_lst)
Can you find the problem in this?

Cheers
Reply
#4
the problem is the [lack of] indentation of line 12. Your loop is infinite
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
To achieve desired result one can combine sets (unordered collection with no duplicate elements) and random.sample (return a k length list of unique elements chosen from the population sequence or set)

>>> import random
>>> nouns = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "gorilla"]
>>> random.sample(set(nouns), 3)
['fossil', 'gorilla', 'chef']   
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thanks all,

I was actually suppose to find a solution using the while loop. I like the "set(), random.sample" solution a lot, so i'll stick to that.
Thank you very much for your time
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020