Search code examples
python-3.xordereddictionary

parse ordered dictionary python


I have an ordered Dictionary:

OrderedDict = 
([('time', ['1', '2', '3', '4', '5', '10', '15', '30', '60', '90']), ('uom', ['kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa'])])

I have a list of gherkin steps:

List =
['Given Device unit of measure is set to value "<uom>"\n', 'And Device is set to value "<time>"\n']

How can i iterate the ordered dictionary so that I can replace "uom" and "time" in the list with values in the ordered dictionary?

Output I want is this:

iteration 1: 
['Given Device unit of measure is set to value 'kpa'\n', 'And Device is set 
to value '1'\n']

iteration 2: 
['Given Device unit of measure is set to value 'kpa'\n', 'And Device is set 
to value '2'\n']

...

iteration 10: 
['Given Device unit of measure is set to value 'kpa'\n', 'And Device is set 
to value '90'\n']

Thank you for your help!


Solution

  • Havin and ordered dict is not important for your task, a simple one is sufficient. An ordered dict is important if you want to get the keys in the same order you stuffed them into the dict.

    You just need the lists of the two keys in question, zip(...) theire value-lists to pairs and print the text-replacement:

    # simple dict
    od = {'time': ['1', '2', '3', '4', '5', '10', '15', '30', '60', '90'], 
          'uom': ['kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa']}
    
    # your text
    texts = ['Given Device unit of measure is set to value "<uom>"\n', 'And Device is set to value "<time>"\n']
    
    someList = []
    
    # zip the correlating uom/time/ppm together and iterate them
    for tup in zip(od["uom"],od["time"]):
        # create a partial list for this iteration and replace the placeholders
        part = [texts[0].replace("<uom>",tup[0]), texts[1].replace("<time>",tup[1])]
        print(part)
    
    # for good measure: stuff into full list
        someList.append(part)
    
    # print full list
    print(someList)
    

    Output:

    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "1"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "2"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "3"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "4"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "5"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "10"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "15"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "30"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "60"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "90"\n']
    
    [['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "1"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "2"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "3"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "4"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "5"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "10"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "15"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "30"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "60"\n'], 
     ['Given Device unit of measure is set to value "kpa"\n', 'And Device is set to value "90"\n']
    ]
    

    Sidenote: your output is not really feasible as you need to switch the ' to '"' if you use them inside a string - or the other way round. You can have

    ["some text 'some other' things"] # or
    ['some text "some other" things'] # or
    # you mask them out by
    ['some text \'some other\' things']
    

    If you do not have the keys up front, you can use dict.keys() to get them. Like so:

    from collections import OrderedDict
    od = OrderedDict([('time', ['1', '2', '3', '4', '5', '10', '15', '30', '60', '90']), 
          ('uom', ['kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa', 'kpa']),
          ('ppm', ['009','019','029','039','049','059','069','079','089','099'])])
    
    # your text
    texts = ['Given Device unit of measure is set to value "<uom>"\n', 'And Device <ppm> is set to value "<time>"\n']
    
    # zip the correlating uom/time together and iterate them
    part = [texts[0], texts[1]] # templated text
    keys = list(od.keys()) # all the keys
    for tup in zip(*od.values()): # all the value tuples (in key order)
        for idx,val in enumerate(tup):
            corrKey = '<'+keys[idx]+'>'  # get corresponding key as replace text
            part[0] = part[0].replace(corrKey,val) # replace it in both parts
            part[1] = part[1].replace(corrKey,val) # of your text
    
        print(part)
        # reset to template text    
        part = [texts[0], texts[1]]
    

    Output:

    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 009 is set to value "1"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 019 is set to value "2"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 029 is set to value "3"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 039 is set to value "4"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 049 is set to value "5"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 059 is set to value "10"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 069 is set to value "15"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 079 is set to value "30"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 089 is set to value "60"\n']
    ['Given Device unit of measure is set to value "kpa"\n', 'And Device 099 is set to value "90"\n']