I have the following dictionary and code, I want to concatenate the values of the dictionary, if the head and tail of the values are the same:
import random
dic={'1':[(1,2,4,3),(1,2,3,1)],'2':[(2,4,2,3),(2,6,5,3)],'3':[(3,5,9,1),(3,2,5,2),(3,7,8,1)]}
c1= (1,2,4,3)
c2=random.choice(dic[str(c1[-1])])
c3=random.choice(dic[str(c2[-1])])
c4=random.choice(dic[str(c3[-1])])
c5=random.choice(dic[str(c4[-1])])
print(c1,c2,c3,c4,c5)
I get a result like this:
(1, 2, 4, 3) (3, 5, 9, 1) (1, 2, 4, 3) (3, 2, 5, 2) (2, 4, 2, 3)
The dictionary has the first element of the tuple as key, for the given c1
, I use the last element of c1
as key to get a random tuple from dic
, by this way, I get c2
,c3
,c4
,c5
.
My question is: I want to write a function with integral input, which could be 10, or 20, so I could get c10
, c20
iteratively, not like now I must write every step of c1
, c2
,c3
,c4
,c5
,c6
....
Simply, how to create variables dynamically>
Any help would be appreciated.