-2

Data = "A good employee like {'A':12345} is essential in today’s world, especially in these tough economic times when there are many seeking work. Consequently, it is important to keep in mind that no employee is irreplaceable like {'B':1234} and {'C':123}"

I want to convert above data to

Data = "A good employee like {'C':123} is essential in today’s world, especially in these tough economic times when there are many seeking work. Consequently, it is important to keep in mind that no employee is irreplaceable like {'B':1234} and {'A':12345}"

Mean to say in the passage has to sort according to values. Is there any way to do this?

aysh
  • 493
  • 1
  • 11
  • 23

1 Answers1

0

First extract the string dictionaries and their positions so we have targets to fill, with enumerate. Then unpack the tuples to get two lists one with indexes and one with the values we will sort. After sort the values using the int of the substring containing the numbers. Then zip the reordered list back with the indexes and re-insert them back into a list of data, and .join() it

vals = [(i, v) for i, v  in enumerate(data.split()) if '{' in v]
x = list(zip(*vals))
x[1] = sorted(x[1], key=lambda x: int(x.strip('}').split(':')[-1]))
y = list(zip(x[0], x[1]))
data = data.split()
for i in y:
    data[i[0]] = i[1]
print(' '.join(data))
A good employee like {'C':123} is essential in today’s world, especially in these tough economic times when there are many seeking work. Consequently, it is important to keep in mind that no employee is irreplaceable like {'B':1234} and {'A':12345}
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20