解析有序字典 python

parse ordered dictionary python

我有一个有序的词典:

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

我有一个小黄瓜步骤列表:

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

如何迭代有序字典,以便我可以用有序字典中的值替换列表中的 "uom" 和 "time"?

我想要的输出是这样的:

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']

感谢您的帮助!

Havin 和 ordered dict 对你的任务来说并不重要,一个简单的就足够了。如果您想按照将键塞入字典的顺序获取键,则有序字典很重要。

您只需要有问题的两个键的列表,zip(...)它们是要配对的值列表并打印文本替换:

# 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)

输出:

['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']
]

旁注:您的输出并不真正可行,因为如果您在字符串中使用它们,则需要将 ' 切换为 '"' - 反之亦然。您可以

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

如果您事先没有钥匙,可以使用 dict.keys() 来获取。像这样:

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]]

输出:

['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']