Finding the average of columns from nested lists in a dictionary's value

Hazim

I have a dictionary with 2 keys (dog and cat):

d = {'dog': [['4.1', '7.0', 'dog'], ['1.2', '3.4', 'dog']], 'cat': [['1', '8.2', '5.501', 'cat'], ['6.5', '8', '9.1', 'cat']]}

I need to find the average of the nested lists of the values for each key. For example, for dog, I need to find the average of 4.1 and 1.2, as well as 7.0 and 3.4.

The problem I'm having is that the nested lists have the numbers as strings. I figured I need an outer while loop to control column number, and then an inner while loop to control row number, however I don't know how to set this up. How do I access the inner list for each key and then access the deeper lists to find the averages?

for keys in d:
    for values in d[keys]:
        (2 while loops here)

Is this how I would go about it?

Thanks.

Ashwani

Assuming value in dictionary contains list of only two elements (which are again lists):

d = {'dog': [['4.1', '7.0', 'dog'], ['1.2', '3.4', 'dog']], 'cat': [['1', '8.2', '5.501', 'cat'], ['6.5', '8', '9.1', 'cat']]}

avg_dict = {}
for key in d:
    avg_list = []
    i = 0
    while (i < len(d[key][0]) - 1):
        avg_list.append((float(d[key][0][i]) + float(d[key][1][i])) /2.0)
        i += 1
    avg_dict[key] = avg_list

print(avg_dict)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the average of columns from nested lists in a dictionary's value

From Dev

What's the correct syntax to populate a Dictionary having nested Lists as value?

From Dev

DataFrame from dictionary with nested lists

From Dev

DataFrame from dictionary with nested lists

From Dev

Dictionary of lists to nested dictionary

From Dev

creating n nested loops from a dictionary of lists

From Dev

Pandas DataFrame from dictionary with nested lists of dictionaries

From Dev

Get average value from list of dictionary

From Dev

Average values in nested dictionary

From Dev

Average values in nested dictionary

From Dev

Average of a nested dictionary

From Dev

Finding the average of specific values in a dictionary

From Dev

Finding the average of specific values in a dictionary

From Dev

Extracting value from nested dictionary

From Dev

finding a specific value from list of dictionary in python

From Dev

JessTab: Finding average value

From Dev

Finding the average value

From Dev

Creating a dictionary from two lists? While taking an average of values?

From Dev

Python--Finding Parent Keys for a specific value in a nested dictionary

From Dev

Finding the average from an array

From Dev

Converting nested lists to dictionary

From Java

Finding highest value in a dictionary

From Dev

Finding minimum value in dictionary

From Dev

Finding highest value in a dictionary

From Dev

Python extract max value from nested dictionary

From Dev

Nested dictionary value from key path

From Dev

How to get value from nested dictionary in python?

From Dev

How do you create a dictionary from nested lists in Python?

From Dev

Finding and printing a value from 2 different lists using LINQ

Related Related

HotTag

Archive