Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary trouble
#6
Your vars is essentially a mapper from full name to possible acronym form(s). Since there are 2 forms at most, and the only difference is a dot at the end, I would suggest reversed mapper with dot-less keys only
Output:
In [143]: acro_2_name = {value: key for key, values in vars.items() ...: for value in values if not value.endswith('.')} In [140]: acro_2_name Out[140]: {'HGB': 'Hemoglobin', 'RBC COUNT': 'RBC count', 'HCT': 'HCT', 'MCV': 'MCV', 'MCH': 'MCH', 'MCHC': 'MCHC', 'WBC COUNT': 'WBC count', 'NEUTROPHIL': 'Neutrophils', 'EOSINOPHIL': 'Eosinophils', 'BASOPHIL': 'Basophils', 'LYMPHOCYTE': 'Lymphocytes', 'MONOCYTE': 'Monocytes', 'AEC': 'AEC', 'PLATELET': 'Platelets'}
When converting results, just remove possible trailing dot - and you are good to go
Output:
In [141]: {acro_2_name[result_acro.rstrip('.')]: value for result_acro, value in d2.items()} Out[141]: {'Basophils': '0.5', 'Eosinophils': '1.1', 'HCT': '32.3', 'Hemoglobin': '10.0', 'Lymphocytes': '16.3', 'MCH': '30.3', 'MCHC': '31.0', 'MCV': '97.9', 'Monocytes': '4.9', 'Neutrophils': '77.2', 'Platelets': '606', 'RBC count': '3.30', 'WBC count': '9.41'}

PS I would suggest to moderate spaces in your code to PEP-8 recommended. Too many spaces/empty lines bloat the code and reduce readability.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
Dictionary trouble - by test - Sep-18-2018, 06:08 AM
RE: Dictionary trouble - by wavic - Sep-18-2018, 06:36 AM
RE: Dictionary trouble - by DeaD_EyE - Sep-18-2018, 07:26 AM
RE: Dictionary trouble - by test - Sep-18-2018, 09:04 AM
RE: Dictionary trouble - by DeaD_EyE - Sep-18-2018, 09:48 AM
RE: Dictionary trouble - by volcano63 - Sep-18-2018, 04:29 PM
RE: Dictionary trouble - by test - Sep-19-2018, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary trouble Involute 3 2,076 Oct-08-2019, 01:32 AM
Last Post: Involute
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,116 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Trouble converting JSON String to Dictionary RBeck22 7 5,269 Mar-28-2019, 12:12 PM
Last Post: RBeck22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020