0

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.

J Arun Mani
  • 620
  • 3
  • 20
  • Your question and description don't match... You want concatenation or dynamic variable creation? – J Arun Mani Jan 28 '20 at 17:55
  • Check this too; https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – J Arun Mani Jan 28 '20 at 17:56
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – J Arun Mani Jan 28 '20 at 18:00
  • You just need the concatenation? I mean, for what I see you don't need to actually have c1, c2...cn but anything else than printing the final concatenation. – revliscano Jan 28 '20 at 18:25
  • yes, I just need the concatenation, but the length of the concatenation should be variable, not just be 5 like in the example – Hongmei Liang Jan 28 '20 at 18:59
  • Alright, I get it. Let me elaborate the answer with a possible solution – revliscano Jan 28 '20 at 19:13
  • I think you should update the name of the question, by the way. "How to create variables on the fly in Python?" doesn't represent what you wanted to achieve. – revliscano Jan 28 '20 at 20:09
  • thanks for the new name – Hongmei Liang Jan 28 '20 at 23:47

3 Answers3

0

So far from understanding your question, I infer that you want an automated way to create variables on the fly, that is have c<n> variables without typing it on your own.

You have two methods:

  1. Using exec: exec is used to execute Python code from the code itself. So you can try something like this:
inp = int(input('How many? ))
for i in range(inp):
    val = 20 # Enter the value here
    exec(f"c{i}=val")

But this is not recommended

  1. Using dictionary: Dictionaries were meant for this and are perfect here
inp = int(input("How many? "))
vals = {}
for i in range(inp):
    val = 10 # Value
    vals[f"c{i}"] = val # or simply vals[i]
J Arun Mani
  • 620
  • 3
  • 20
  • val = 10 is a constant, but for every c0, c1, c2, ...I need them to be different, and c1 is calculated depending on c0, c2 is calculated depending on c1, c3 is calculated depending on c2, ...usw – Hongmei Liang Jan 28 '20 at 18:58
  • vals = {} for i in range(inp): if i==0: vals[f"c{0}"] = (1,2,4,3) #i>0 else: vals[f"c{i}"] = random.choice(dic[str(vals[f"c{i-1}"][-1])]) – Hongmei Liang Jan 28 '20 at 19:07
0

I think using recursion is a nice approach to solve this.

import random

def concatenator(dictionary, i, key=1):
    if i > 0:
        tuple_chosen = random.choice(dictionary[str(key)])
        next_key = tuple_chosen[-1]
        return str(tuple_chosen) + concatenator(dictionary, i-1, key=next_key)
    else:
        return ""

Using this function as follows: (i is the number of tuples you want to have at the end)

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)]}

concatenator(dic, 5)
# outputs: '(1, 2, 3, 1)(1, 2, 3, 1)(1, 2, 3, 1)(1, 2, 3, 1)(1, 2, 3, 1)'
revliscano
  • 2,227
  • 2
  • 12
  • 21
0

You can do this with a list comprehension using the assignment expression operator := in Python 3.8.

>>> 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)]}
>>> y = (1,2,4,3)
>>> n = 20
>>> [(y:=random.choice(dic[str(y[-1])])) for _ in range(n)]
[(3, 2, 5, 2), (2, 4, 2, 3), (3, 7, 8, 1), (1, 2, 4, 3), (3, 2, 5, 2), (2, 4, 2, 3), (3, 7, 8, 1), (1, 2, 4, 3), (3, 5, 9, 1), (1, 2, 3, 1), (1, 2, 3, 1), (1, 2, 3, 1), (1, 2, 4, 3), (3, 2, 5, 2), (2, 4, 2, 3), (3, 7, 8, 1), (1, 2, 3, 1), (1, 2, 3, 1), (1, 2, 3, 1), (1, 2, 4, 3)]

The initial choice of y doesn't really matter, as long as str(y[-1]) is a key in dic.

chepner
  • 497,756
  • 71
  • 530
  • 681