rkimura
rkimura

Reputation: 5

Set a tuple of strings as a dict key

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}

Upvotes: 0

Views: 243

Answers (1)

Jethro Cao
Jethro Cao

Reputation: 1050

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}

Upvotes: 1

Related Questions