Danny Garcia
Danny Garcia

Reputation: 227

Print one word from a list within a dictionary (python)

I want my program to print only one random word from the list within my dictionary, but I can't seem to get the right syntax to do so. I attempted using popitem() to get a random value from the list, but it doesn't seem to be working. Here is my code:

import random

thesaurus = {
              "happy":["glad",  "blissful", "ecstatic", "at ease"],
              "sad"  :["bleak", "blue", "depressed"]
            }

# input
phrase = input("Enter a phrase: ")

# turn input into list
part1 = phrase.split()
part2 = list(part1)

newlist = [] 
for x in part2:
    s = thesaurus.get(x, x)
    newlist.append(s)

print (newlist)

For example, if the input is

i am happy

The expected output would be

i am glad

or any random word from the list within the dictionary.

But, right now my output looks like this:

['i', 'am', ['glad', 'blissful', 'ecstatic', 'at ease']]

I know there is another thread involved with this, but it doesn't seem to address this specific issue.

Any help would be appreciated!

edit:

If I extended this formula to work with an imported file with a long list of words, how would I have to change the code?

newDict = {}
with open('thesaurus.txt', 'r') as f:
    for line in f:
        splitLine = line.split()
        newDict[(splitLine[0])] = ",".join(splitLine[1:])

print ("Total words in thesaurus: ", len(newDict))

# input
phrase = input("Enter a phrase: ")

# turn input into list
part1 = phrase.split()
part2 = list(part1)

# testing input
newlist = []
for x in part2:
    s = newDict[x].pop() if x in newDict else x
    s = random.choice(newDict[x]).upper() if x in newDict else x
    newlist.append(s)


newphrase = ' '.join(newlist)
print (newphrase)

sample of line text within the "thesaurus" file:

abash,humility,fear

Upvotes: 4

Views: 1669

Answers (4)

Colin Yang
Colin Yang

Reputation: 455

This solution does not modify your original dictionary

for x in part2:
    s = random.choice(thesaurus.get(x, [x]))
    newlist.append(s)

Your dictionary maps a string to list of strings, therefore your original solution puts a list in the place of a string. random.choice chooses a random element from the list.

Upvotes: 0

mattsap
mattsap

Reputation: 3806

map your output to this. You can join the list together to form a string of you want too.

newList= list(map(lambda x:  random.choice(x) if type(x) == list else x, newList))

print(" ".join(newList))

Upvotes: 1

smac89
smac89

Reputation: 43264

You might want to make use of the random module:

Example:

import random
>>> l = list(range(10))
>>> random.choice(l)
5
>>> random.choice(l)
9

In your case, you could do:

print (" ".join(random.choice(thesaurus[x]) if x in thesaurus else x for x in part2))

Example:

>>> import random
>>> phrase = "I am feeling sad that he left, but that's okay because I'm happy he will be back soon"
>>>
>>> thesaurus = { "happy":["glad",  "blissful", "ecstatic", "at ease"],
...               "sad"  :["bleak", "blue", "depressed"]
...             }
>>> print (" ".join(random.choice(thesaurus[x]) if x in thesaurus else x for x in phrase.split()))
I am feeling bleak that he left, but that's okay because I'm blissful he will be back soon

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 114108

 thesaurus.get(x,x)

means thesaurus[x] if x in thesaurus else x

since thesaurus["happy"] is a list it returns the whole list

I think you want to just get a single item

for x in part2:
   s = thesaurus[x].pop() if x in thesaurus else x # returns first word (and removes from list)
   s = thesaurus[x][0] if x in thesaurus else x # returns first word without removing it
   s = random.choice(thesaurus[x])if x in thesaurus else x # returns random word
   newlist.append(s)

Upvotes: 2

Related Questions