samedi 20 mai 2017

Why does random.sample() add square brackets and single quotes to the item sampled?

I'm trying to sample an item (which is one of the keys in a dictionary) from a list and later use the index of that item to find its corresponding value (in the same dictionary).

questions= list(capitals.keys())
answers= list(capitals.values())
for q in range(10):
    queswrite = random.sample(questions,1)
    number = questions.index(queswrite)
    crtans = answers[number]

Here,capitals is the original dectionary from which the states(keys) and capitals(values) are being sampled.

But,apparently random.sample() method adds square brackets and single quotes to the sampled item and thus prevents it from being used to reference the list containing the corresponding values.

the terminal response to the code above

How can I prevent this?




Aucun commentaire:

Enregistrer un commentaire