2

I have 3 lists of prices:

iexMin =  [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
withLimit =  [0.649, 1.298, 1.298, 2.538, 2.596, 2.596, 2.560, 2.560, 0.560, 1.682, 1.682, 2.242, 2.242, 2.242, 2.287, 2.592, 2.388, 2.98, 3.29, 3.299]
beyondLimit =  [0.66, 2.30134, 2.30155, 2.30171, 2.955, 2.51, 2.519, 2.51, 1.749, 1.749, 1.749, 1.745, 1.749, 1.82208, 1.8993, 1.899, 2.29657, 2.29659, 2.29692, 2.30931]

I am dividing them into 4 groups of 5 elements and after that sorting them in decreasing order to get the minimum first 4 min values from each list and then get the compare all the min value from all the list:

n = 5
a = [iexMin[i:i + n] for i in range(0, len(iexMin), n)]
b = [withLimit[i:i + n] for i in range(0, len(iexMin), n)]
c = [beyondLimit[i:i + n] for i in range(0, len(iexMin), n)]

testList1 = [sorted(block)[:4] for block in a]
testList2 = [sorted(block)[:4] for block in b]
testList3 = [sorted(block)[:4] for block in c]

price1 = [item for t in testList1 for item in t]
price2 = [item for t in testList2 for item in t]
price3 = [item for t in testList3 for item in t]

minRate = [min(price1[i],price2[i],price3[i]) for i in range(len(price1))]

output:

min rate =  [0.649, 1.298, 1.298, 2.30171, 0.56, 1.682, 2.51, 2.51, 1.682, 1.749, 1.749, 1.82208, 1.899, 2.29657, 2.29659, 2.29692]

The only problem with this output is that I am not comparing the whole matrix here. As I stated that at the beginning that I am dividing the lists into 4 blocks of 5 elements, so basically I want to compare the whole block at once and the 5 min values from each block. So expected output will look like this:

IEXMin = [[20, 20, 20, 20], [20, 20, 20, 20], [20, 20, 20, 20], [20, 20, 20, 20]]
withinLimit = [[0.649, 1.298, 1.298, 2.538], [0.56, 1.682, 2.56, 2.56], [1.682, 2.242, 2.242, 2.242], [2.388, 2.592, 2.98, 3.29]]
beyondLimit = [[0.66, 2.30134, 2.30155, 2.30171], [1.749, 1.749, 2.51, 2.51], [1.745, 1.749, 1.749, 1.82208], [1.899, 2.29657, 2.29659, 2.29692]]

##Expected Answer##
min rate =  [0.649, 0.66, 1.298, 1.298, 0.56, 1.682, 1.749, 1.749, 1.682, 1.745, 1.749, 1.749, 1.899, 2.29657, 2.29659, 2.29692]

So as you can see I actually want to compare the block at a time and get the first 4 min value from each block. Can someone please help?

1 Answer 1

2

I think you did some extra work and hence made things complex. Here's one way to solve it:

Using your code:

n = 5
a = [iexMin[i:i + n] for i in range(0, len(iexMin), n)]
b = [withLimit[i:i + n] for i in range(0, len(iexMin), n)]
c = [beyondLimit[i:i + n] for i in range(0, len(iexMin), n)]

Then,

minRate = [sorted(a+b+c)[:4] for a,b,c in zip(a, b, c)]
minRate = [item for each in minRate for item in each]
print(minRate)

Output: [0.649, 0.66, 1.298, 1.298, 0.56, 1.682, 1.749, 1.749, 1.682, 1.745, 1.749, 1.749, 1.899, 2.29657, 2.29659, 2.29692]

The mistake in your original code was that you were comparing the elements at the same index in the three lists (the last line), and then you'd increase the index.

Update: How does sorted(a+b+c) work?

Firstly, the a,b,c in the list comprehension correspond to the blocks of the lists a, b, c that you defined. I just found that reusing a,b,c more convenient but I sure could have used better names.

Now that out of the way, the + operator actually joins lists together. [1,1]+[1,1] will give [1,1,1,1], for instance. So I concatenate the blocks into one and then sort them and pick the lowest 4- sorted(a+b+c)[:4].

Word of caution when using numpy: Using + operator with numpy arrays will instead add them as vectors. Since here I could infer from your comprehensions that it was not a numpy array, I used the Python list operator + to get the desired result.

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

2 Comments

hey thank you so much but I just have one doubt, if you could help me in that then that would more helpful. How does that sorted(a+b+c) work? I mean how does that searching the min value from each block?
I hope the update to the answer clears your doubt :)

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.