0

I have a dictionary of frozensets keys and values:

{(frozenset(['Age = 70', 'SMOK = y', 'LAD = 75']), frozenset(['CHOL = 220'])): 1.0, (frozenset(['AL = 0.0', 'DIAB = y', 'LAD = 75']), frozenset(['LM = 30'])): 1.0}

How can I convert it to a normal dictionary like this?

{('(Age = 70, LAD = 40)', '(LM = 15)'): '1.0', ('(SEX = F, AL = 0.2, RCA = 85)', '(LM = 15)'): '1.0'}
user91
  • 365
  • 5
  • 14
  • Would be a bit clearer if your output dictionary data matched your input dictionary data. Even better if you included some attempt at your own code with an explanation of what is not working. May also help if you explain just a bit more about how you want to transform the current dictionary keys. Looks like you may be trying to convert tuples of frozensets to tuples of corresponding strings but I am not sure. – benvc Apr 30 '19 at 18:57
  • Neither the input nor the expected output are normal. Having to structure data in such (ugly) way is an indicator of bad design. Also, there's no (obvious) rule how to convert elements from input to output. – CristiFati Apr 30 '19 at 19:27
  • you sure about the output format? I think it is ugly and werid in that way. – recnac May 04 '19 at 13:39

1 Answers1

0

if you just want to convert frozen set to tuple, you can try this:

d1 = {(frozenset(['Age = 70', 'SMOK = y', 'LAD = 75']), frozenset(['CHOL = 220'])): 1.0, (frozenset(['AL = 0.0', 'DIAB = y', 'LAD = 75']), frozenset(['LM = 30'])): 1.0}

d2 = {tuple(map(tuple, k)): v for k, v in d1.items()}

output

{(('SMOK = y', 'LAD = 75', 'Age = 70'), ('CHOL = 220',)): 1.0, (('DIAB = y', 'AL = 0.0', 'LAD = 75'), ('LM = 30',)): 1.0}

if you want the format exactly like your sample output, you can try this:

d3 = {tuple(map(lambda x: f'({x})', map(', '.join, map(tuple, k)))): str(v) for k, v in d1.items()}

output: (exactly alike your output format)

{('(SMOK = y, LAD = 75, Age = 70)', '(CHOL = 220)'): '1.0', ('(DIAB = y, AL = 0.0, LAD = 75)', '(LM = 30)'): '1.0'}

But as others said, the input and output format are werid in that way, maybe you should ravel out your requirement first.

recnac
  • 3,744
  • 6
  • 24
  • 46