For cyclic alternating tuples

I'm new to Python (and going back to coding 20 years later ...)

I am making a small script to learn irregular english verbs. I create my dict which looks like this (it's in French)

verbes = {
"abide":["abode","abode","respecter / se conformer à"],
"arise":["arisen","arisen","survenir"],
}

      

if i want to iterate over the verbs i do for example:

for verb, (pret, past, trad) in verbes.items():
    print "Le verbe %s se conjugue au prétérit par %s, au participe passé par %s et se traduit par %s" % (verb, pret, past, trad)

      

Good that works. Then I found out that I can import a random value to pick one random key and value in my dictionary

rand = random.choice(verbes.items())

      

If I print rand, I have a key, value, but if I repeat again, I have a traceback:

Traceback (most recent call last):
File ".\exverbe1.py", line 209, in <module>
    for verb, (pret, past, trad) in rand.items(): AttributeError: 'tuple' object has no attribute 'items'

      

I am not involved in the "tuple" tutorial yet, but I tried to turn it into a list, dict, etc. without success. I have searched "for tuple" in stackoverflow with no result. So now I am asking for help 3 hours later trying to figure this out ... Thanks

+3


source to share


2 answers


random.choice(verbes.items())

selects one random case from verbes.items()

. rand

therefore contains one element - rand[0]

contains key and rand[1]

value.

If what you are trying to do is iterate over the elements in the list of values:

for val in rand[1]:
  print val

      



To unpack a tuple rand

into its constituents:

verb, (pret, past, trad)

      

verb

now contains the key and pret

, past

and trad

each item in the list corresponding to the key.

+2


source


rand

is an element from verbes

, so there is no need to call elements again:

rand = random.choice(verbes.items())
verb, (pret, past, trad) = rand

      

dict.items()

returns a list of "items" in a dict. Each element is a tuple of the form (key, value)

. random.choice

returns one of these tuples. I see that you are trying to cross rand

, maybe you need more than one random element? If so, you can do something like:

for i in range(N):
     verb, (pret, past, trad) = random.choice(verbes.items())
     # Do more stuff

      

Update

In python they are called asigment like a, b = iterable

unpacking. You can use many different types on the right side, for example:



# A tuple
a, b = ("A", "B")
# A list
a, b = ["A", "B"]
# A string
a, b = "AB"

      

In the simple case, the number or variables must be the same as the number of elements in the iterable or python will throw an error. You can unpack it like this:

 a, (b, c, d) = ("A", "BCD")
 # is the same as
 a, temp = ("A", "BCD")
 (b, c, d) = temp

      

()

outside are optional, so is the a, b = item

same as (a, b) = item

.

Python 3 introduces even more complex unboxing types that I never use, but you can read about them here .

+1


source







All Articles