1

This is my list :

vec = [[(0, 6.369426751591834e-05),
      (1, 6.369426751591834e-05),
      (2, 6.369426751591834e-05),
      (3, 6.369426751591834e-05),
      (4, 6.369426751591834e-05),
      (5, 6.369426751591834e-05),
      (6, 6.369426751591834e-05),
      (7, 0.0007714083510261222),
      (8, 6.369426751591834e-05),  
      ...........................
      (4995, 6.369426751591834e-05),
      (4996, 6.369426751591834e-05),
      (4997, 6.369426751591834e-05),
      (4998, 6.369426751591834e-05),
      (4999, 6.369426751591834e-05)]]

I want to sort it based 2nd column which has float value. Plus how to print first 10 from the output list. I tried many methods, but couldn't figure out. Any suggestions?

2
  • 2
    try this sorted(vec[0], key=lambda x: x[1]) Commented Mar 25, 2014 at 15:11
  • Paulo, it was output of lda algorithm, it was nested by default. Commented Mar 25, 2014 at 15:15

4 Answers 4

3
sorted(vec[0], key=lambda x: x[1])

vec[0] only because you have a nested list... normally it would just be vec

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

2 Comments

This is a TypeError; key must be a keyword argument
Thanks for the answer, but above one by @Grijesh worked. I tried yours, it gives :TypeError: <lambda>() takes exactly 1 argument (2 given)
2

Generally, you can sort by the ith value in a container using operator.itemgetter(i) as the key for sorting:

from operator import itemgetter

vec[0] = sorted(vec[0], key=itemgetter(1))

On your example, with some duplicates removed:

>>> vec = [[(0, 6.369426751591834e-05),
            (7, 0.0007714083510261222),
            (8, 6.369426751591834e-05)]]
>>> sorted(vec[0], key=itemgetter(1))
[(0, 6.369426751591834e-05), 
 (8, 6.369426751591834e-05), 
 (7, 0.0007714083510261222)]

2 Comments

A little overkill. @d_rez90's is simpler
This one worked too! Thanks. I can't upvote your replies guys, i don't have enough points.
2

numpy way of doing it:

In [7]:
AV=np.array(vec)
AV[:,np.argsort(AV[:,:,1])]
Out[7]:
array([[[[  0.00000000e+00,   6.36942675e-05],
         [  1.00000000e+00,   6.36942675e-05],
         [  2.00000000e+00,   6.36942675e-05],
         [  3.00000000e+00,   6.36942675e-05],
         [  4.00000000e+00,   6.36942675e-05],
         [  5.00000000e+00,   6.36942675e-05],
         [  6.00000000e+00,   6.36942675e-05],
         [  8.00000000e+00,   6.36942675e-05],
         [  4.99500000e+03,   6.36942675e-05],
         [  4.99600000e+03,   6.36942675e-05],
         [  4.99700000e+03,   6.36942675e-05],
         [  4.99800000e+03,   6.36942675e-05],
         [  4.99900000e+03,   6.36942675e-05],
         [  7.00000000e+00,   7.71408351e-04]]]])

There is a np.set_printoptions() for controlling print options. There is no way, however, to only print the first 10 lines, you can instead print the first n lines and the last n lines by: np.set_printoptions(threshold=a_samll_number, edgeitems=n)

1 Comment

Thanks, that printing option is really handy for observing first and last ten results.
1

As you asked to print the first ten:

srtd = sorted(vec[0], lambda x: x[1])
print srtd[:10]

6 Comments

Put a minus in front of x[1] if you want to flip sort order.
I already tried vec[:10], it prints the whole list in my case
That's because your list is nested. Above will work as it takes out the first element. You would need vec[0][:10]. Or simply don't nest the list inside another list.
yes, it works after adding key: as pointed above: sorted(vec[0], key=lambda x: x[1])
When you declare it, you are declaring it with two square brackets. Use one. Then you can remove the [0] from a lot of the solutions. The numpy one works well as he is extracting the inner list first with: [:,:,1]. However, that seems a little overkill. My modification of @d_rev90's is the simplest approach.
|

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.