Home > Mobile >  Set a tuple of strings as a dict key
Set a tuple of strings as a dict key

Time:09-17

I need to create a dictionary where the keys are a tuple of strings and the values are integers. I have done it using the CSV module but with pandas, I can better manage my data.

Basically I want this:

print(Supply)
{(1, 1): 400, (1, 2): 0, (2, 1): 1500, (2, 2): 0, (3, 1): 900, (3, 2): 0}

To become this:

print(Supply)
{('1', '1'): 400, ('1', '2'): 0, ('2', '1'): 1500, ('2', '2'): 0, ('3', '1'): 900, ('3', '2'): 0}

CodePudding user response:

This does the transformation you want:

Supply = {tuple(str(i) for i in key): val for key, val in Supply.items()}

print(Supply)

# {('1', '1'): 400, ('1', '2'): 0, ('2', '1'): 1500, ('2', '2'): 0, ('3', '1'): 900, ('3', '2'): 0}
  • Related