Re: A random word from one of two lists
On 1/3/21 11:34 AM, Richard Damon wrote: It depends on what distribution of results you want. Since the example had two equal length list is doesn't matter, but if, say, there were many more animals then fruit, your method would produce an animal more often than a fruit, but the two level method will make the distribution 50% fruits, 50% animals independent on the number in each category. Which is the right answer is problem specific. quite true! -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 1/3/21 12:38 PM, Mats Wichmann wrote: > On 1/3/21 5:30 AM, Bischoop wrote: >> On 2021-01-02, Stefan Ram wrote: >>> Bischoop writes: On 2021-01-02, Stefan Ram wrote: > Otherweise, I'd go this way without a dictionary. > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > kinds = [animal,fruit] > kind = random.choice( kinds ) > result = random.choice( kind ) > print( result ) I had that solution in mind but I thought that one is not good programming style or not Pythonin :-) >>> >>> I do not see any stylistic problem when you use this approach >>> with "nested lists". List indexing by a number should even be >>> faster than indexing a dictionary. >>> >>> >> Now I know that's ok, seems I was ovethingking while solution was so >> simply. >> >> -- >> Thanks >> > > You don't really need to do this as a two-level selection: > > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > result = random.choice(animal + fruit) > print(result) It depends on what distribution of results you want. Since the example had two equal length list is doesn't matter, but if, say, there were many more animals then fruit, your method would produce an animal more often than a fruit, but the two level method will make the distribution 50% fruits, 50% animals independent on the number in each category. Which is the right answer is problem specific. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 1/3/21 5:30 AM, Bischoop wrote: On 2021-01-02, Stefan Ram wrote: Bischoop writes: On 2021-01-02, Stefan Ram wrote: Otherweise, I'd go this way without a dictionary. import random animal = ['koala', 'kangaroo'] fruit = ['banana', 'apple'] kinds = [animal,fruit] kind = random.choice( kinds ) result = random.choice( kind ) print( result ) I had that solution in mind but I thought that one is not good programming style or not Pythonin :-) I do not see any stylistic problem when you use this approach with "nested lists". List indexing by a number should even be faster than indexing a dictionary. Now I know that's ok, seems I was ovethingking while solution was so simply. -- Thanks You don't really need to do this as a two-level selection: import random animal = ['koala', 'kangaroo'] fruit = ['banana', 'apple'] result = random.choice(animal + fruit) print(result) -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 2021-01-02, Stefan Ram wrote: > Bischoop writes: >>On 2021-01-02, Stefan Ram wrote: >>>Otherweise, I'd go this way without a dictionary. >>>import random >>>animal = ['koala', 'kangaroo'] >>>fruit = ['banana', 'apple'] >>>kinds = [animal,fruit] >>>kind = random.choice( kinds ) >>>result = random.choice( kind ) >>>print( result ) >>I had that solution in mind but I thought that one is not good >>programming style or not Pythonin :-) > > I do not see any stylistic problem when you use this approach > with "nested lists". List indexing by a number should even be > faster than indexing a dictionary. > > Now I know that's ok, seems I was ovethingking while solution was so simply. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 2021-01-02, Stefan Ram wrote: > > The following might /not/ be good programming style, > but addresses the idea to choose a "variable". > I kinda like the below one. > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > kinds = ['animal','fruit'] > variable = random.choice( kinds ) > result = random.choice( globals()[ variable ]) > print( result ) > > Otherweise, I'd go this way without a dictionary. > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > kinds = [animal,fruit] > kind = random.choice( kinds ) > result = random.choice( kind ) > print( result ) > I had that solution in mind but I thought that one is not good programming style or not Pythonin :-) -- Thanks > -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 2021-01-02, Python wrote: > > >>> from random import choice > >>> choice(words[choice(list(words.keys()))]) > 'apple' > >>> choice(words[choice(list(words.keys()))]) > 'kangaroo' > >>> choice(words[choice(list(words.keys()))]) > 'koala' > >>> choice(words[choice(list(words.keys()))]) > 'apple' > >>> choice(words[choice(list(words.keys()))]) > 'kangaroo' > > or this? > > >>> randstuff = lambda: choice(words[choice(list(words.keys()))]) > >>> randstuff() > 'kangaroo' > >>> randstuff() > 'kangaroo' > >>> randstuff() > 'apple' > >>> randstuff() > 'kangaroo' > >>> randstuff() > 'koala' > >>> randstuff() > 'banana' that lambada method is good. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 2021-01-01, Cameron Simpson wrote: > > kinds = list(words.keys()) > Yes, solved it with that exeactly. -- thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
Try this: >>> animal = ['koala', 'kangaroo'] >>> fruit = ['banana', 'apple'] >>> words = {'animals': animal, 'fruits': fruit} >>> kinds = tuple(words.keys()) >>> kind = random.choice(kinds) >>> kind 'animals' >>> word = random.choice(words[kind]) >>> word 'koala' -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 01Jan2021 22:58, Bischoop wrote: >I have two lists *animal* and *fruit* and I try to get a random variable >animal or fruit and then random word from that list. >I thought that dictionary(*words*) could help me but no succes, the only way >I've >done partially what was with list *kinds* and by using two times >random.choice. However print(kind) return me a list not a variable: >animal or fruit so now I'm kinda confuse if I can achieve that. > >import random >animal = ['koala', 'kangaroo'] >fruit = ['banana', 'apple'] This is fine. >kinds = [animal,fruit] This probably isn't what you intend. Print out kinds: print(repr(kinds)) >words = {'animals': animal, 'fruits': fruit} I suspect you want, after this line: kinds = list(words.keys()) which gets you a list of the dictionary keys. Print that, too, to see the difference. >kind = random.choice(kinds) >print(kind) When the earlier stuff works, this should be a string. >word = random.choice(kind) This picks a random character from the string. Probably not what you want. You want to pick from the list of animals or fruits. Since you (will) have the word "animals" or "fruits" in "kind", how do you reference the appropriate list? Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: A random word from one of two lists
On 1/1/21 5:58 PM, Bischoop wrote: > I have two lists *animal* and *fruit* and I try to get a random variable > animal or fruit and then random word from that list. > I thought that dictionary(*words*) could help me but no succes, the only way > I've > done partially what was with list *kinds* and by using two times > random.choice. However print(kind) return me a list not a variable: > animal or fruit so now I'm kinda confuse if I can achieve that. > > > > import random > animal = ['koala', 'kangaroo'] > fruit = ['banana', 'apple'] > kinds = [animal,fruit] > words = {'animals': animal, 'fruits': fruit} > > > kind = random.choice(kinds) > print(kind) > word = random.choice(kind) > print(word) > #output: > ['kolala', 'kangaroo'] > kangaroo > > -- > Thanks random.choice doesn't return 'a variable' but an object. Which is the same object that one of the variables that you used. IF you wanted to use words, then you could have made kinds = {'animals', 'fruits'} and then looked up the list in words, but that is really just more work (unless you want to know which list it came from) Note that 'animals' isn't a variable, but a string value. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
A random word from one of two lists
I have two lists *animal* and *fruit* and I try to get a random variable animal or fruit and then random word from that list. I thought that dictionary(*words*) could help me but no succes, the only way I've done partially what was with list *kinds* and by using two times random.choice. However print(kind) return me a list not a variable: animal or fruit so now I'm kinda confuse if I can achieve that. import random animal = ['koala', 'kangaroo'] fruit = ['banana', 'apple'] kinds = [animal,fruit] words = {'animals': animal, 'fruits': fruit} kind = random.choice(kinds) print(kind) word = random.choice(kind) print(word) #output: ['kolala', 'kangaroo'] kangaroo -- Thanks -- https://mail.python.org/mailman/listinfo/python-list