Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary trouble
#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


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,098 Oct-08-2019, 01:32 AM
Last Post: Involute
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,141 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Trouble converting JSON String to Dictionary RBeck22 7 5,316 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