Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Arrays in Ascending
#1
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]

Hi , I just want to Sort the above in Ascending order , All are 1D arrays
Reply
#2
What have you tried or at least thought about?
Reply
#3
Something like
from operator import itemgetter

your_list = ... # list of 1-D lists

your_list.sort(key=itemgetter(0))
That said, as ndc85430 said, your posts should generally show your attempt.
Reply
#4
There are MANY ways to do this but you haven't given us any idea what your actual goal is. There are no base "arrays" in Python. You have shown us 9 python lists, each with 1 float number in them. If you want to sort them, put them all into a list and use the built-in sorted() or list.sort() function.

https://docs.python.org/2/howto/sorting....rtinghowto
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#5
Maybe I am stupid but what is wrong with just:

>>> sorted([[1.1], [1.0], [1.05]])
[[1.0], [1.05], [1.1]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Jan-10-2020, 07:35 PM)perfringo Wrote: Maybe I am stupid
For the OP's purposes, yours is probably equivalent to mine, and as you implied, simpler. In retrospect I should have used a lambda instead of a mysterious built-in library too.
Reply
#7
Thanks
Actually my problem is to list the smallest Cosine distance in Graph
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)] # these are the list of x & y axix
P= (3,-4) # so here i have to find the distance between each tuple in S with P & find the top 5 lowest distance axis

below is the code i have written to find the cosine distance between each Tuple in S & P .


import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
for i in range (0,len(S)):
#print(S[i])
#print(np.ndim(S[i]))
J=np.reshape(S[i],(1,2))
#print(J)
#print(np.ndim(J))
P=[(3,-4)]
#print(np.ndim(P))
A=cosine_similarity(J,P)
#print(A)
C=1-A # Cosine Distance = 1-cosine similarity
#print©
#print(np.ndim©)
D=np.reshape(C,(1))
#print(np.ndim(D))
print(D)

# if i print D , i am getting the below Cosine distance
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]
now i want to Sort 5 distance in ascending order (Less distance) & to print the respective X & Y axis
Reply
#8
Actually my problem is to list the smallest Cosine distance in Graph
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)] # these are the list of x & y axix
P= (3,-4) # so here i have to find the distance between each tuple in S with P & find the top 5 lowest distance axis

below is the code i have written to find the cosine distance between each Tuple in S & P .


import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
for i in range (0,len(S)):
#print(S[i])
#print(np.ndim(S[i]))
J=np.reshape(S[i],(1,2))
#print(J)
#print(np.ndim(J))
P=[(3,-4)]
#print(np.ndim(P))
A=cosine_similarity(J,P)
#print(A)
C=1-A # Cosine Distance = 1-cosine similarity
#print©
#print(np.ndim©)
D=np.reshape(C,(1))
#print(np.ndim(D))
print(D)

# if i print D , i am getting the below Cosine distance
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]
now i want to Sort 5 distance in ascending order (Less distance) & to print the respective X & Y axis
Reply
#9
(Jan-10-2020, 06:47 PM)Marbelous Wrote: There are MANY ways to do this but you haven't given us any idea what your actual goal is. There are no base "arrays" in Python. You have shown us 9 python lists, each with 1 float number in them. If you want to sort them, put them all into a list and use the built-in sorted() or list.sort() function.

https://docs.python.org/2/howto/sorting....rtinghowto

Thanks
Actually my problem is to list the smallest Cosine distance in Graph
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)] # these are the list of x & y axix
P= (3,-4) # so here i have to find the distance between each tuple in S with P & find the top 5 lowest distance axis

below is the code i have written to find the cosine distance between each Tuple in S & P .


import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
for i in range (0,len(S)):
#print(S[i])
#print(np.ndim(S[i]))
J=np.reshape(S[i],(1,2))
#print(J)
#print(np.ndim(J))
P=[(3,-4)]
#print(np.ndim(P))
A=cosine_similarity(J,P)
#print(A)
C=1-A # Cosine Distance = 1-cosine similarity
#print©
#print(np.ndim©)
D=np.reshape(C,(1))
#print(np.ndim(D))
print(D)

# if i print D , i am getting the below Cosine distance
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]
now i want to Sort 5 distance in ascending order (Less distance) & to print the respective X & Y axis
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an ascending number [SOLVED] AlphaInc 3 2,204 Jul-11-2021, 10:13 AM
Last Post: perfringo
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 2,997 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020