How do I make a dictionary with a repeating value pattern?

Viewed 67

I am trying to create dictionary with a reapeating pattern like

{0:"A",
1:"B",
2:"C",
3:"D",
4:"A",
5:"B",
6:"C",
7:"D",}

and so on. How would I do that? I have tried using for loops, but couldn't figure it out.

I'm not even sure this is the right approach to my problem. I am solving a simulation numerous times with the same output, only changing 1 input for every loop of the simulation. Basically I end up with a DataFrame that collects the output (4 different series) for every simulation with columns

[0, 1, 2, 3, 4, 5, 6, 7, 8, ...]

which I would like to rename

["A", "B", "C", "D", "A", "B", "C", "D",...]

Alternatively, is there some sort of datatype in Python, which can provide 2 levels of categorizing like

[Simulation 1: ["A", "B", "C", "D"],
Simulation 2: ["A", "B", "C", "D"],
Simulation 3: ["A", "B", "C", "D"],
Simulation 4: ["A", "B", "C", "D"],
Simulation 5: ["A", "B", "C", "D"],
and so on...]

where "A", "B", "C" and "D" each contains a column of data output, that is different for every simulation?

3 Answers

You can achieve this neatly with itertools.cycle:

In [1]: import itertools

In [2]: cols = [0, 1, 2, 3, 4, 5, 6, 7]

In [3]: dict(zip(cols, itertools.cycle('ABCD')))
Out[3]: {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D'}

If you'd rather not import modules you could use dictionary comprehension with a modulus operator (%)

print({i:'ABCD'[i%4] for i in range(12)})

{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'A', 9: 'B', 10: 'C', 11: 'D'}

If you want to use a for-loop, you could use the modulo operator along with string.ascii_uppercase:

>>> from string import ascii_uppercase
>>> n = 8
>>> repeat_every = 4
>>> d = {i: ascii_uppercase[i % repeat_every] for i in range(n)}
>>> d
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D'}

Alternatively, is there some sort of datatype in Python, which can provide 2 levels of categorizing like...

You could use itertoools.permutations inside a dict comprehension:

>>> from itertools import permutations
>>> from string import ascii_uppercase
>>>
>>> def pretty_print_simple_dict(d):
...     print("{")
...     for k, v in d.items():
...         print(f"\t{k}: {v}")
...     print("}")
...
>>> repeat_every = 4
>>> d = {
...     f"Simulation {i + 1}": list(p)
...     for i, p in enumerate(permutations(ascii_uppercase[:repeat_every]))
... }
>>>
>>> pretty_print_simple_dict(d)
{
    Simulation 1: ['A', 'B', 'C', 'D']
    Simulation 2: ['A', 'B', 'D', 'C']
    Simulation 3: ['A', 'C', 'B', 'D']
    Simulation 4: ['A', 'C', 'D', 'B']
    Simulation 5: ['A', 'D', 'B', 'C']
    Simulation 6: ['A', 'D', 'C', 'B']
    Simulation 7: ['B', 'A', 'C', 'D']
    Simulation 8: ['B', 'A', 'D', 'C']
    Simulation 9: ['B', 'C', 'A', 'D']
    Simulation 10: ['B', 'C', 'D', 'A']
    Simulation 11: ['B', 'D', 'A', 'C']
    Simulation 12: ['B', 'D', 'C', 'A']
    Simulation 13: ['C', 'A', 'B', 'D']
    Simulation 14: ['C', 'A', 'D', 'B']
    Simulation 15: ['C', 'B', 'A', 'D']
    Simulation 16: ['C', 'B', 'D', 'A']
    Simulation 17: ['C', 'D', 'A', 'B']
    Simulation 18: ['C', 'D', 'B', 'A']
    Simulation 19: ['D', 'A', 'B', 'C']
    Simulation 20: ['D', 'A', 'C', 'B']
    Simulation 21: ['D', 'B', 'A', 'C']
    Simulation 22: ['D', 'B', 'C', 'A']
    Simulation 23: ['D', 'C', 'A', 'B']
    Simulation 24: ['D', 'C', 'B', 'A']
}
Related