Python Forum
Extracting random word from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting random word from list
#1
I created a little program here, and I'm having trouble with this part ---
        var1 = random.choice(ggg[0])
        var2 = random.choice(ggg[1])
        var3 = random.choice(ggg[2]) 
I'm attempting to get var1, var2, and var3 to pull a random word from each set of the list named 'ggg' (see below), but the [] keeps pulling a single character instead of a word. When I emit the [], it pulls everything between the brackets instead. Any ideas on how to get it to work properly? Below is the full program. Insert this to make it work : ###{ hello | hi } /{ goodbye | bye } /{ eek | meep }[/python]### --- Again, I am trying to get it to randomly select one word from each set and randomly mish-mash when I click. ALMOST THERE!

from tkinter import *
import random
import re
import secrets

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        self.instruction = Label(self, text = "Enter Jumbo")
        self.instruction.grid(row = 0, column=0, columnspan = 3, sticky = W)

        self.text = Text(self, width = 35, height =5, wrap = WORD)
        self.text.grid(row =1, column=1, sticky = W)

        self.submit_button = Button(self, text = "Submit", command = self.reveal)
        self.submit_button.grid(row = 2, column = 0, sticky =W)

        self.text1 = Text(self, width = 35, height =5, wrap = WORD)
        self.text1.grid(row = 3, column =0, columnspan = 2, sticky=W)
    def reveal(self):
        content = self.text.get("1.0", "end-1c")
        answer = str(content)
        ggg = answer.split("/")
             

        
        var1 = random.choice(ggg[0])
        var2 = random.choice(ggg[1])
        var3 = random.choice(ggg[2])

        
        print (content)
        print (answer)
        print (ggg)
        print (var1)
        print (var2)
        print (var3)
    

      


        self.text1.insert(3.0, var1, var2, var3)
        
root = Tk()
root.title("practice")
root.geometry("450x250")
app = Application(root)

root.mainloop()
Reply
#2
Quote:
random.choice(ggg[0])
if ggg is a list of words, then ggg[0] is the first word. Then random choice will select a random character from that word. You programmed it that way

IF you want just a random word in the list you would od
random.choice(ggg)
If your intention was to make nested lists
I would of probably made a button to add another entry for each list, then select randomly from each list as opposed to making the user define the separation
Recommended Tutorials:
Reply
#3
I was fiddling with both white space and delimiters. But what you recommended still doesn't work; it just randomly selects a set and prints the entire thing, {hi|hello} for example. What I want it to do is randomly print "hi" or "hello" from within the {} and divided by the |.
Reply
#4
I was just editing when you responded.

You need to either
  • make code to separate { hello | hi } /{ goodbye | bye } /{ eek | meep } into the proper format expected
  • since you are using GUI, separate each entry of words into a new entry. So one entry would be hello hi while the next would be goodbye bye. then just split by whitespace, and its already separated.
I personally thin that latter the best approach.
Recommended Tutorials:
Reply
#5
Can you point me in the right direction of "write code to separate"? That's where I'm finding myself befuddled. What would I use to separate the text?
Reply
#6
something like
import tkinter as tk

def addBox():
    ent = tk.Entry(root)
    ent.pack()
    all_entries.append( ent )

def showEntries():
    for number, ent in enumerate(all_entries):
        print(number, ent.get())

all_entries = []

root = tk.Tk()

showButton = tk.Button(root, text='Show all text', command=showEntries)
showButton.pack()

addboxButton = tk.Button(root, text='<Add Time Input>', fg="Red", command=addBox)
addboxButton.pack()

root.mainloop()
Recommended Tutorials:
Reply
#7
Then just add the split for whitespace

Attached Files

Thumbnail(s)
   
Recommended Tutorials:
Reply
#8
So enumerate? I'l give it a shot! Thanks for the advice.
Reply
#9
enumerate was just to show you the index at which the list would be at in the all_entries list. IN my picture
all_entries[0] would be 'hello hi' so you could do
random.choice(all_entries[0].split()) then. where 0 would be the first field, 1 the second field, and so on and so on.
Recommended Tutorials:
Reply
#10
Yeah, I think we're walking on two different planes here. I'm going to be inputting a LARGE token, not just a few words. Think :

{{howdy|hey|hey there|heyyy|hi cutey|hi there =)|sup|heyy|hey hey|heyyy|hiya|heya|heyyyy|hello there|hey babes|hey sexy|hey heyy|hi there}{..|.|,|...} {thanks|thnx|thank you|ty} {4|for} the {follow|add}{...|.|..} {whats up|what's up|what is up|what you up to|what u up to|what ya up to|how ya doing|how you dhow ya doin|how you doin|whats going on|what is going on|what's going on|what is goin on|whats goin on|what's goin on|how are ya|how are you|how are u}{?|??|???} {nm|not much|chilling|chillin|relaxing|relaxin|hanging out|hangin out|chillin out|chilling out|lounging out|coolin out} {here|over here}{.|,} {txt me|t xt me|tx t me|t.xt me|tx.t me|t.x.t me|text me|t ext me|te xt me|tex t me|t.ext me|te.xt me|tex.t me}{...|} {i am|im|i'm} {downright|flat out|terribly|extremely|really|super|absolutely|truly} bored{.|..|...} %s {..|....|..}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 462 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,251 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,723 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,528 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,621 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,548 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,517 Aug-12-2021, 04:25 PM
Last Post: palladium
  Extracting Elements From A Website List knight2000 2 2,286 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Unable to use random.choice(list) in async method spacedog 4 3,449 Apr-29-2021, 04:08 PM
Last Post: spacedog
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 1,936 Feb-10-2021, 11:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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