So I was wondering if there was a much more elegant solution to the one I have implemented right now into merging values of an ordered dict.
I have an ordered dict that looks like this
'fields': OrderedDict([
("Sample Code", "Vendor Sample ID"),
("Donor ID", "Vendor Subject ID"),
("Format", "Material Format"),
("Sample Type", "Sample Type"),
("Age", "Age"),
("Gender", "Gender"),
("Ethnicity/ Race", "Race"),
]),
If I pass in a parameter like so as a list
[2,3] or [2,4,5]
is there an elegant way to merge the values together under a new key so
[2,3], "Random_Key"
would return
'fields': OrderedDict([
("Sample Code", "Vendor Sample ID"),
("Donor ID", "Vendor Subject ID"),
**("Random Key", "Material Format Sample Type"),**
("Age", "Age"),
("Gender", "Gender"),
("Ethnicity/ Race", "Race"),
]),
while also deleting the keys in the dictionary?
This can also be done nicely with a generator.
This generator yields the key item pair if it doesn't have to be squashed, and if it has, it saves the items till the last entry, and then yields it, with a new key and the saved items joined.
With the generator a new OrderedDict can be constructed.
from collections import OrderedDict
def sqaushDict(d, ind, new_key):
""" Takes an OrderedDictionary d, and yields its key item pairs,
except the ones at an index in indices (ind), these items are merged
and yielded at the last position of indices (ind) with a new key (new_key)
"""
if not all(x < len(d) for x in ind):
raise IndexError ("Index out of bounds")
vals = []
for n, (k, i), in enumerate(d.items()):
if n in ind:
vals += [i]
if n == ind[-1]:
yield (new_key, " ".join(vals))
else:
yield (i, k)
d = OrderedDict([
("Sample Code", "Vendor Sample ID"),
("Donor ID", "Vendor Subject ID"),
("Format", "Material Format"),
("Sample Type", "Sample Type"),
("Age", "Age"),
("Gender", "Gender"),
])
t = OrderedDict(squashDict(d, [2, 3], "Random"))
print(t)