Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:31:10+00:00 2026-05-20T17:31:10+00:00

I have 2 list containing dictionaries as follows: listone = [{‘unit1’: {‘test1’: 10}}, {‘unit1’:

  • 0

I have 2 list containing dictionaries as follows:

listone = [{'unit1': {'test1': 10}}, 
           {'unit1': {'test2': 45'}, 
           {'unit2': {'test1': 78'}, 
           {'unit2': {'test2': 2'}}]

listtwo = [{'unit1': {'test1': 56}}, 
           {'unit1': {'test2': 34'}, 
           {'unit2': {'test1': 23'}, 
           {'unit2': {'test2': 5'}}]

I also do have all the unit names & test-names in separate lists:

units = ['unit1', 'unit2']
testnames = ['test1,'test2']

How could I find the delta for each test value, i.e. val of (test2 – test1), so that I could finally arrange the data as follows:

unit1, test1, delta
unit1, test2, delta
unit2, test1, delta
unit2, test2, delta

So far, I have these:

def delta(array1, array2):
        temp = []
        temp2 = []
        tmp = []
        tmp2 = []
        delta = []
        for unit in units:
            for mkey in array1:
                for skey in mkey:
                    if skey == unit:
                        temp.append(mkey[skey])
                        floater(temp) #floats all the values
                        for i in testnames:
                            for u in temp:
                                tmp.append(u[i])
                                tmp = filter(None, tmp2)

            for mkey in array2:
                for skey in mkey:
                    if skey == unit:
                        temp.append(mkey[skey])
                        floater(temp2)
                        for i in testnames:
                            for u in temp2:
                                tmp2.append(u[i])
                                tmp2 = filter(None, tmp2)

        delta = [tmp2 - tmp for tmp2, tmp in zip(tmp2, tmp)] 
        print delta

delta(listone,listtwo)

Unfortunately, the code gives Keyerror. 🙁
Help, please. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:31:10+00:00Added an answer on May 20, 2026 at 5:31 pm

    Similar but a bit more encapsulated:

    from collections import defaultdict
    
    listone = [
        {'unit1': {'test1': 10}},
        {'unit1': {'test2': 45}}, 
        {'unit2': {'test1': 78}}, 
        {'unit2': {'test2': 2}}
    ]
    
    listtwo = [
        {'unit1': {'test1': 56}},
        {'unit1': {'test2': 34}}, 
        {'unit2': {'test1': 23}}, 
        {'unit2': {'test2': 5}}
    ]
    
    def dictify(lst):
        res = defaultdict(lambda: defaultdict(int))
        for entry in lst:
            for unit,testentry in entry.iteritems():
                for test,val in testentry.iteritems():
                    res[unit][test] = val
        return res
        # returns dict['unitX']['testY'] = val
    
    def genDeltas(dictA, dictB):
        units = dictA.keys()
        units.sort()
        tests = dictA[units[0]].keys()
        tests.sort()
        for unit in units:
            _A = dictA[unit]
            _B = dictB[unit]
            for test in tests:
                yield unit,test,(_B[test]-_A[test])
    
    for unit,test,delta in genDeltas(dictify(listone),dictify(listtwo)):
        print "{0}, {1}, {2}".format(unit,test,delta)
    

    Edit: to find field-averages:

    class Avg(object):
        def __init__(self, total=0.0, num=0):
            super(Avg,self).__init__()
            self.total = total
            self.num   = num
    
        def add(self, value):
            self.total += value
            self.num   += 1
    
        def value(self):
            return self.total / self.num
    
    def avgBy(data, field=0):
        res = defaultdict(Avg)
        for unit,testdict in data.iteritems():
            for test,val in testdict.iteritems():
                res[(unit,test)[field]].add(val)
        return {item:avg.value() for item,avg in res.iteritems()}
    
    dictone = dictify(listone)
    avg_by_unit = avgBy(dictone, 0)
    print(avg_by_unit)
    avg_by_test = avgBy(dictone, 1)
    print(avg_by_test)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list containing 2 dictionaries: >>> x [{'sby_0': 'sb0'}, {'sby_1': 'sb1'}] I
Essentially, I have a very large List containing in turn relatively large Dictionaries. So
I have a list containing objects of type numpy.ndarray, all list elements has same
I have a list of dictionaries containing unicode strings. csv.DictWriter can write a list
I have a list containing a tuples and long integers the list looks like
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
I have a list containing about 1000 file names to search under a directory
Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
I have a dropdown list containing the days of the week - Monday to
I have a stl::list containing Widget class objects. They need to be sorted according

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.