Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary trouble
#1
Hello!
I am having a dictionary of the type:
	vars = {	'Hemoglobin' 	: ['HGB'],
				'RBC count' 	: ['RBC COUNT', 'RBC COUNT.'],
				'HCT'				: ['HCT', 'HCT.'],
				'MCV'			: ['MCV', 'MCV.'],
				'MCH'			: ['MCH', 'MCH.'],
				'MCHC'			: ['MCHC', 'MCHC.'],
				'WBC count'	: ['WBC COUNT'],
				'Neutrophils'	: ['NEUTROPHIL'],
				'Eosinophils'	: ['EOSINOPHIL', 'EOSINOPHIL.'],
				'Basophils'		: ['BASOPHIL'],
				'Lymphocytes'	: ['LYMPHOCYTE', 'LYMPHOCYTE']
				'Monocytes'		: ['MONOCYTE'],
				'AEC'				: ['AEC'],
				'Platelets'			: ['PLATELET', 'PLATELET.']
}
I have another dictionary which contains the values of these parameters, with the parameter label set to one variable which is in the list, like:
d2 = {'BASOPHIL': '0.5',
 'EOSINOPHIL.': '1.1',
 'HCT.': '32.3',
 'HGB': '10.0',
 'LYMPHOCYTE.': '16.3',
 'MCH.': '30.3',
 'MCHC.': '31.0',
 'MCV.': '97.9',
 'MONOCYTE': '4.9',
 'NEUTROPHIL': '77.2',
 'PLATELET.': '606',
 'RBC COUNT.': '3.30',
 'WBC COUNT': '9.41'}
I have to input these values into an excel sheet in which the variable names are set to the key values in vars.
I am unable to think of any way to do it. Am i right in using dictionaries to do this task? What is the best way to do this?
Reply
#2
You can't iterate over the dicts or you don't know how to put all into an excel spreadsheet?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Do you want to have a result like this:

{'Hemoglobin': ['10.0'],
 'RBC count': ['-', '3.30'],
 'HCT': ['-', '32.3'],
 'MCV': ['-', '97.9'],
 'MCH': ['-', '30.3'],
 'MCHC': ['-', '31.0'],
 'WBC count': ['9.41'],
 'Neutrophils': ['77.2'],
 'Eosinophils': ['-', '1.1'],
 'Basophils': ['0.5'],
 'Lymphocytes': ['-', '-'],
 'Monocytes': ['4.9'],
 'AEC': ['-'],
 'Platelets': ['-', '606']}
You can do this with a dict comprehension in combination with a list comprehension in one line. But you should first do it by creating two nested loops. The frist loop should iterate over the first dict and create an empty list. In the inner loop you iterate over the values (list) of the first dict. Use the get(item, '-') method, to get the values from the second dict. When the inner loop has been finished, you put the result in a new dict.

I did not do the complete task. I guess it's better, when you solve the problem.
Look up for documentation about dictionaries in the Python documentation.
The get Method is very useful. It can also return default values.

result_dict = {}
for key, values in vars.items():
   result = []
   for value in values:
       real_value = d2.get(value, '-')
# code not complete
I hope I did not too much.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Hello sir,
Thank you so much.
The following code worked for me:
vars = {	'Hemoglobin' 	: ['HGB'], 'RBC count' : ['RBC COUNT', 'RBC COUNT.'], 'HCT': ['HCT', 'HCT.'], 'MCV'	: ['MCV', 'MCV.'], 'MCH'	: ['MCH', 'MCH.'], 	'MCHC'	: ['MCHC', 'MCHC.'], 	'WBC count'	: ['WBC COUNT'], 	'Neutrophils'	: ['NEUTROPHIL'], 	'Eosinophils'	: ['EOSINOPHIL', 'EOSINOPHIL.'], 	'Basophils'	: ['BASOPHIL'], 	'Lymphocytes'	: ['LYMPHOCYTE', 'LYMPHOCYTE'],	'Monocytes': ['MONOCYTE'], 	'AEC': ['AEC'], 	'Platelets'	: ['PLATELET', 'PLATELET.'] }
d2 = {'BASOPHIL': '0.5',  'EOSINOPHIL.': '1.1',  'HCT.': '32.3',  'HGB': '10.0',  'LYMPHOCYTE.': '16.3',  'MCH.': '30.3',  'MCHC.': '31.0',  'MCV.': '97.9',  'MONOCYTE': '4.9',  'NEUTROPHIL': '77.2',  'PLATELET.': '606',  'RBC COUNT.': '3.30',  'WBC COUNT': '9.41'} 

result_dict = {}
for key, values in vars.items():
	result = []
	for value in values:
		real_value = d2.get(value, '-')
		result_dict[key] = real_value
		
print(result_dict)
Reply
#5
Good :-)

Maybe you understand this:
{key: [d2.get(value, '-') for value in indigrends] for key, indigrends in vars.items()}
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Comprehensions.html
https://docs.python.org/3/tutorial/datas...ctionaries

I needed long time to understand this, but it's nearly the same like the loop above.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
#7
Thank you DeaD_EyE, volcano63 for your time :)
@volcano63, thanks for the tip, I'll follow the spacing recommendations henceforth. And, i needed a dictionary for storing alternate variables because i might have to add more of them later, Sir. The other ones might be quite different from the ones already there. Hence i followed DeaD_EyE's approach.

@DeaD_EyE, sir, i dont right away understand how the one line code works. I think it'll become clearer the more i use python. Thanks a ton for your input :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary trouble Involute 3 2,011 Oct-08-2019, 01:32 AM
Last Post: Involute
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,046 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Trouble converting JSON String to Dictionary RBeck22 7 5,109 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