0

I have the two inputs and my output

Sorted_Array = ['Historic Rate', 'Overnight', '1M', '3M', '6M', '1Y', '2Y', '3Y', '4Y', '5Y', '6Y', '7Y', '8Y', '9Y', '10Y', '12Y', '15Y']

Input = ['6M', '2Y', '7Y', '1Y']

Output = ['7Y', '1Y', '6M', '2Y']

the output is unintuitive, it should be

Actual_Output = ['6M','1Y','2Y','7Y']

the code I use is:

Ouput = [x for _, x in sorted(zip(Sorted_Array,Input), key=lambda pair: pair[0])]
print(Output)

Can anyone see where this has gone wrong?

3
  • Why would you expect that output? I think your code is working properly.... Commented Oct 2, 2018 at 10:02
  • 1
    you are sorting your sorted list, not using it to sort the the second list Commented Oct 2, 2018 at 10:04
  • Have you looked at what zip(Sorted_Array,Input) looks like ? The output is what one would expect. I don't know what you're trying to do but obviously you're doing it wrong ;-) Commented Oct 2, 2018 at 10:15

3 Answers 3

2

You are currently pairing the items of Input with the 4 first items of Sorted_Array:

>>> temp = list(zip(Sorted_Array, Input)))
[('Historic Rate', '6M'), ('Overnight', '2Y'), ('1M', '7Y'), ('3M', '1Y')]

Then you sort with a lambda function based on the first items of these tuples. Which is in the following order:

>>> sorted(temp, key=lambda pair: pair[0])
[('1M', '7Y'), ('3M', '1Y'), ('Historic Rate', '6M'), ('Overnight', '2Y')]

It should now be clear how you end up with the result you have. To sort based on the order of the elements in Sorted_Array can the index function:

>>> sorted(Input, key=lambda item: Sorted_Array.index(item))
['6M', '1Y', '2Y', '7Y']

Note: the index function raises an exception if the item is not present in the list.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean raises a ValueError if there is no such item present in the list.
Thanks, I didn't realise I was doing this
0

sorted(zip(Sorted_Array,Input), key=lambda pair: pair[0]) sort the result based on the key, the first member of the pair, from which you extracted the second member

>>>> Output = [x for x in sorted(zip(Sorted_Array,Input), key=lambda pair: pair[0])]
>>>> print(Output)
[('1M', '7Y'), ('3M', '1Y'), ('Historic Rate', '6M'), ('Overnight', '2Y')]
>>>> Output = [x for _, x in sorted(zip(Sorted_Array,Input), key=lambda pair: pair[0])]
>>>> print(Output)
['7Y', '1Y', '6M', '2Y']

you need to change the key to pair[1]

>>>> Output = [x for _, x in sorted(zip(Sorted_Array,Input), key=lambda pair: pair[1])]
>>>> print(Output)
['1Y', '2Y', '6M', '7Y']

2 Comments

That's not the expected output, the OP says it should be ['6M','1Y','2Y','7Y'].
well, i'd say the OP expected it to be intuitive, which I'd argue sorted by the actual first char 1, 2, 6 7 is more intuitive than using the 2nd char M, Y, Y, Y. But my actual intention is to pointed out the usage of key=lamda pair: pair[0] there (in line with his question: Can anyone see where this has gone wrong?).
0

in python 2.x:

def dist(x,y):
     return sorted_array.index(x) - sorted_array.index(y)

output = sorted(input, cmp=dist)

In python 3.x:

def func(input):
    output = input
    for i in range(len(output)):
         try:
            output[i] = sorted_array.index(output[i])
         except Exception as e:
            output[i] = len(sorted_array)+1
    return output

output = [ x for x,y in sorted((input,func(input),key = lambda k: k[1])]

this should work

3 Comments

the sorted-array containing the order you are following to sort
You mean what the OP called Sorted_Array?
yes it is the Sorted_array of OP. i ll change it for better understanding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.