0

I have a text file and I need to assign a random word from this text file (each word is on a separate line) to a variable in Python. Then I need to remove this word from the text file.

This is what I have so far.

with open("words.txt") as f:    #Open the text file
    wordlist = [x.rstrip() for x in f]
variable = random.sample(wordlist,1)     #Assigning the random word
print(variable)
2
  • Does what you have work? If not, what does not work? Commented Apr 16, 2015 at 11:38
  • i managed to assign a random word from the text file to a variable, however i am now struggling on how to remove that random word from the text file Commented Apr 16, 2015 at 11:41

3 Answers 3

1

Use random.choice to pick a single word:

variable = random.choice(wordlist)

You can then remove it from the word list by another comprehension:

new_wordlist = [word for word in wordlist if word != variable]

(You can also use filter for this part)

You can then save that word list to a file by using:

with open("words.txt", 'w') as f:    # Open file for writing
    f.write('\n'.join(new_wordlist))

If you want to remove just a single instance of the word you should choose an index to use. See this answer.

Sign up to request clarification or add additional context in comments.

12 Comments

I suspect he also needs to write the new_wordlist back out to words.txt, although I'm reaching the limits of my mind-reading ability here. :)
I've added the information he may need, but the intial answer does answer the question in the title.
Why not word_list.remove(variable)?
@ReutSharabani: The title isn't what matters; it's the actual question that you have to answer. (But you do answer that, and quite nicely, which is why I upvoted.)
The new version at the end is wrong. randint is a closed interval, not half-open. So this will appear to work for many runs, and then one day raise an IndexError. (As the docs for randint and randrange say about randint, "in Python this is usually not what you want")
|
1

If you need to handle duplicates, and it's not acceptable to reshuffle the list every time, there's a simple solution: Instead of just randomly picking a word, randomly pick an index. Like this:

index = random.randrange(len(wordlist))
word = wordlist.pop(index)
with open("words.txt", 'w') as f:
    f.write('\n'.join(new_wordlist))

Or, alternatively, use enumerate to pick both at once:

word, index = random.choice(enumerate(wordlist))
del wordlist[index]
with open("words.txt", 'w') as f:
    f.write('\n'.join(new_wordlist))

Comments

0

Rather than random.choice as Reut suggested, I would do this because it keeps duplicates:

random.shuffle(wordlist) # shuffle the word list 
theword = wordlist.pop() # pop the first element

4 Comments

How do you know it's acceptable to reshuffle the list every time?
@abarnert What do you mean by acceptable? A list can always be shuffled.
Of course a list can always be shuffled, but then it's a different list. For example, if the original list was in some particular meaningful order, and you want to be able to open it in a text editor and skim through it, well, now you can't, and that might not be acceptable.
@MalikBrahimi I've added a way to prevent shuffle and keep duplicates in my answer that picks an index rather than a word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.