0

I did a list of x elements (a, b, c,...x,) and I now want to turn them into variable like a = 2. This is so I can later call them in my code by their name since I want to do some operation like a + b. This might be the wrong way to do it but I just can't find a solution.

For those interested, I'm doing this since I want a program that can calculate the chance % of a player to win a card tournament with a line up of different decks. This is my code so far, "Listemu" is the list I then want to turn elements into variables (like, "deck0deck01 = 60").

nbdeck = input("Nombre de deck :")
nbdeck = int(nbdeck)
i = 0
Listedeck=[]
while i < nbdeck :
    Listedeck.append('deck'+str(i))
    i += 1
i = 0
a = 0
Listemu=[]
for elt in Listedeck :
    while i < nbdeck :
        Listemu.append('deck'+str(a)+'deck'+str(i))
        i += 1
    a += 1
    i = 0

Here I want to insert every % of two decks match up like deck0 against deck1 = 60% for deck0 (created in Listemu under "deck0deck1" name) to call the % when I simulate the confrontation.

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 1
    This is exactly what `dictionaries` are there for. They are used to assign values to keys. Your keys would be `a`, `b`, `c`, ... `x` and your values can be about anything. From then on whenever you need the value instead of the key, you use the `dict` to look them up. – offeltoffel Feb 04 '19 at 14:13
  • How do you want the values to be assigned? – DirtyBit Feb 04 '19 at 14:15
  • Also: [Python: assign values to variables in a list or object](https://stackoverflow.com/questions/8989801/python-assign-values-to-variables-in-a-list-or-object) – Georgy Feb 04 '19 at 14:35
  • Also: [How to assign values to variables in a list in Python?](https://stackoverflow.com/questions/53652427/how-to-assign-values-to-variables-in-a-list-in-python) – Georgy Feb 04 '19 at 14:36

1 Answers1

0

You have a list of elements and I am assuming you have a list of values already. What you need is a dictionary that will create a key-value pair.

Something like:

elem_list = ['a','b','c','d','e']
val_list = [1,2,3,4,5]

dictionary_ = dict(zip(elem_list, val_list))
print(dictionary_)

OUTPUT:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

and then:

#access the value of a dictionary with its key
print(dictionary_["a"])   # output: 1

you could then even assign it to a variable:

a = dictionary_["a"]   # a = 1
DirtyBit
  • 16,613
  • 4
  • 34
  • 55