Draw an item from the List, and then draw another item from a list with the name that was drawn from the first list

Asked

Viewed 1,445 times

0

Good morning, everyone. I have a master list called "Livros".
I need to draw a name from that list Livros.

After choosing one of the names, I need to draw again, but now I need to choose from another list, and this second list has the name of the one that was drawn first.

To clarify.
on the List Livros have ("Lucas" e "Isaias")

Suppose the random choice was Lucas So my program has to make another random choice within that other call list Lucas that has the values Lucas1 e Lucas2.

Lastly, let’s suppose you enter "Lucas1" e "Lucas2" was chosen the "Lucas1"

Downstairs I have a dictionary called "Lucas1", com as chaves "1", "2" e "3" the last thing I want It’s that my program chooses one of these keys and prints its value. Chose "1" : "Text1" I want you to print this.. Then I’ll adapt it to whatever I need. I tried several ways, researched but could not find the solution. I thank all

#!/usr/bin/env python3
from random import *

#Turple Ordered and Unchangeable
Livros = ("Lucas", "Isaias")


Lucas = ("Lucas1", "Lucas2")
Isaias = ("Isaias1", "Isaias2")

Lucas1 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}

Lucas2 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4" 
}

Isaias1 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}


Isaias2 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}


print("How to Random ? ")
  • Did you do anything else or just create the lists and stuff?

  • I tried several things, tried some methods, but they didn’t work, they drew the list first correctly, but then in the second draw only brought me a letter... I researched , but nothing like what I’m trying to do. Summing up I tried and didn’t get anywhere, it was more complicated than I imagined it would be

  • for example here, he picks the first list and then he picks himself a letter from that book, but I didn’t want it like that. LivroEscolha = choice(Livros)
print(LivroEscolha)

CapEscolha = choice(LivroEscolha)
print(CapEscolha)

1 answer

0


A suggestion is to delete this intermediate between the lists, so you don’t need to have something that links the "Lucas" of a table with the "Lucas1" "Lucas2" of others.

You can do this by making lists of lists, and go on doing Random.Choice sequentially on each layer of them. For this I only had to modify the order in which they were declared, and modified to contain the elements mentioned above in the code.

In the lower layer, you chose to use a dictionary. In this case, as you need to do Random.Choice on a list, we can do Random only on list(dicionario.keys()) and take the key or value related to this draw. See the example code:

#!/usr/bin/env python3
import random

Lucas1 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}

Lucas2 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4" 
}

Isaias1 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}

Isaias2 = {
    "1" : "Text1" ,
    "2" : "Text2" ,
    "3" : "Text3" ,
    "4" : "Text4"
}

Lucas = (Lucas1, Lucas2)
Isaias = (Isaias1, Isaias2)

Livros = (Lucas, Isaias)

NomeEscolhido=random.choice(Livros)
SegundaEscolha=random.choice(NomeEscolhido)
entradaNoDicionarioEscolhida=random.choice(list(SegundaEscolha.keys()))
#print(entradaNoDicionarioEscolhida) #Caso queira printar a chave sorteada
print(SegundaEscolha[entradaNoDicionarioEscolhida]) #Caso queira printar o valor

Who returned in an execution:

Text1
  • Perfect, that’s exactly what I was looking for, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.