python merge list of dictionaries based on key

userBZ

I am looking for a python alternative to a join.

I am trying to get a list with every second of the day, and join data to that, based on the timestamp. What I have so far, is this:

keys=('DRIP_ID','DESCR','OBJECT','TIMESTAMP','DRIP_R1','DRIP_R2','RT_DISP1','RT_DISP2','DAY','TIME')

The keys are the column names

rawdata=[['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701063825','N242','N508','10','14','20150701','063825'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701064327','N242','N508','10','14','20150701','064327'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701085717','N242','N508','10','14','20150701','085717'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701100116','N242','N508','10','14','20150701','100116'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701191611','N242','N508','10','14','20150701','191611'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel -  - ${pijlop} N242  10 min - N508  14 min${pijlr}",'BD242418','20150701213616','N242','N508','10','14','20150701','213616']]

The rawdata is what comes out of the software

sec = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59']
mm = sec
hh = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
timestamp=()
time = []
dictData = []

# Dictionary with all seconds (HHMMSS) in 1 day
for ih, uur in enumerate(hh):
    if ih < 24:
          for im, minutes in enumerate(mm):
              if im < 60:
                  for isec, secs in enumerate(sec):
                      if isec < 60:
                        timestamp = str(uur)+str(minutes)+str(secs)
                        timeDict = dict()
                        timeDict['DRIP_ID']=""
                        timeDict['DESCR']=""
                        timeDict['OBJECT']=""
                        timeDict['TIMESTAMP']=""
                        timeDict['DRIP_R1']=""
                        timeDict['DRIP_R2']=""
                        timeDict['RT_DISP1']=""
                        timeDict['RT_DISP2']=""
                        timeDict['DAY']=""
                        timeDict['TIME']=timestamp
                        time.append(timeDict)

Here I made all the seconds in a day and gave them the same keys, for easier matching

# Turn raw data into dictionary                        
for row in rawdata:
    dictionary = dict(zip(keys, row))
    dictData.append(dictionary)

Then I take the rawdata and turn that into a dict as well

#Join, sort off
compleet=()
for t in time:
    t.update(dictData)
    compleet.append(t)

print len(compleet)
print compleet[1]

However, when I run this, I get the error:

ValueError: dictionary update sequence element #0 has length 10; 2 is required

Which lead me to believe that I can only update key:value pair at a time, but I am not sure this is correct.

Furthermore: it's a 1:1 join. 1 timestamp can have only 1 measuerement. Not every second in a day has a measurement. The 'Join on' would be on "TIME"

userBZ
#Same result as a join, by iterating.
for iTime, t in enumerate(time):
    for iData, d in enumerate(dictData):
        if t['TIME'] == d['TIME']:
            t.update(d)

After realizing what went wrong, and seeing no join, this was the best next thing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

inside list of dictionaries, merge lists based on key

From Java

python filter list of dictionaries based on key value

From Dev

Unpacking a list of dictionaries based on key matches in Python

From Dev

Filter a list of dictionaries in Python based on key

From Dev

Merge python dictionaries with common key

From Dev

Sorting a list of dictionaries based on a key

From Dev

How to categorize list of dictionaries based on the value of a key in python efficiently?

From Dev

Merging a list of dictionaries in python based on one key/value pair?

From Dev

How to categorize list of dictionaries based on the value of a key in python efficiently?

From Dev

To merge two dictionaries of list in Python

From Dev

nicer way to merge list of dictionaries by key

From Dev

nicer way to merge list of dictionaries by key

From Java

Intersection of two list of dictionaries based on a key

From Dev

Intersection of two list of dictionaries based on a key

From Dev

Summarize a list of dictionaries based on common key values

From Dev

Sort python list of dictionaries by key if key exists

From Dev

Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary

From Dev

Python - Filter list of dictionaries based on if key value contains all items in another list

From Dev

merge key, value list based on key values

From Dev

How to merge a list of dictionaries into a dictionary key:list pairing?

From Dev

merge two dictionaries by key

From Dev

Merge dictionaries with key combinations

From Dev

Merge dictionaries with key combinations

From Dev

merge two dictionaries by key

From Dev

remove dictionaries from list based on key value from nested dictionary in python

From Dev

Using ansible to merge object values with the same key in a list of dictionaries variable

From Dev

Python deduplicate list of Dictionaries by Value of a Key

From Dev

python check if dictionaries value of a key duplicate in list

From Dev

Python - Convert List Of Dictionaries & Key Values To String

Related Related

  1. 1

    inside list of dictionaries, merge lists based on key

  2. 2

    python filter list of dictionaries based on key value

  3. 3

    Unpacking a list of dictionaries based on key matches in Python

  4. 4

    Filter a list of dictionaries in Python based on key

  5. 5

    Merge python dictionaries with common key

  6. 6

    Sorting a list of dictionaries based on a key

  7. 7

    How to categorize list of dictionaries based on the value of a key in python efficiently?

  8. 8

    Merging a list of dictionaries in python based on one key/value pair?

  9. 9

    How to categorize list of dictionaries based on the value of a key in python efficiently?

  10. 10

    To merge two dictionaries of list in Python

  11. 11

    nicer way to merge list of dictionaries by key

  12. 12

    nicer way to merge list of dictionaries by key

  13. 13

    Intersection of two list of dictionaries based on a key

  14. 14

    Intersection of two list of dictionaries based on a key

  15. 15

    Summarize a list of dictionaries based on common key values

  16. 16

    Sort python list of dictionaries by key if key exists

  17. 17

    Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary

  18. 18

    Python - Filter list of dictionaries based on if key value contains all items in another list

  19. 19

    merge key, value list based on key values

  20. 20

    How to merge a list of dictionaries into a dictionary key:list pairing?

  21. 21

    merge two dictionaries by key

  22. 22

    Merge dictionaries with key combinations

  23. 23

    Merge dictionaries with key combinations

  24. 24

    merge two dictionaries by key

  25. 25

    remove dictionaries from list based on key value from nested dictionary in python

  26. 26

    Using ansible to merge object values with the same key in a list of dictionaries variable

  27. 27

    Python deduplicate list of Dictionaries by Value of a Key

  28. 28

    python check if dictionaries value of a key duplicate in list

  29. 29

    Python - Convert List Of Dictionaries & Key Values To String

HotTag

Archive