By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

sorting

P: 9
I am very new to python ... I am using windows xp

i am writing a program in python , in which i want to sort the first elements of say 5 arrays

eg :
Expand|Select|Wrap|Line Numbers
  1. a = [3,2,1]
  2. b=[7,5,3]
  3. c=[56,242,4]
  4. r=[3,146,124]
so i used zip to make them in to a single array
Expand|Select|Wrap|Line Numbers
  1. t= zip(a,b,c,d)
which = >
t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r(2))

now i used y=t(:1)
=> y = (a[0],b[0],c[0])


now if i say
y.sort()
print y


the result is y but its not gettin sorted
Nov 20 '07 #1
Share this Question
Share on Google+
8 Replies


bartonc
Expert 5K+
P: 6,596
I am very new to python ... I am using windows xp

i am writing a program in python , in which i want to sort the first elements of say 5 arrays

eg :
Expand|Select|Wrap|Line Numbers
  1. a = [3,2,1]
  2. b=[7,5,3]
  3. c=[56,242,4]
  4. r=[3,146,124]
so i used zip to make them in to a single array
Expand|Select|Wrap|Line Numbers
  1. t= zip(a,b,c,d)
which = >
t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r(2))

now i used y=t(:1)
=> y = (a[0],b[0],c[0])


now if i say
y.sort()
print y


the result is y but its not gettin sorted
I didn't try zip(). I simply add the lists together:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = a + b + c + r
  7. print t
  8. # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
  9. t.sort()
  10. print t
  11. # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242]
Nov 20 '07 #2

P: 9
I didn't try zip(). I simply add the lists together:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = a + b + c + r
  7. print t
  8. # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
  9. t.sort()
  10. print t
  11. # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242]



here what i'm trying to do is ......
i want to sort the elements

a[o],b[0].c[0]

thats all ... i dont want to sort all the arrays ...
Nov 20 '07 #3

bartonc
Expert 5K+
P: 6,596
I didn't try zip(). I simply add the lists together:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = a + b + c + r
  7. print t
  8. # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
  9. t.sort()
  10. print t
  11. # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242]
If zip()ing all the nth items together is what you are really after, then this will work:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = zip(a, b, c, r)
  7. print t
  8. # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)]
  9. for i, intTuple in enumerate(t):
  10.     t[i] = sorted(intTuple)
  11. print t
  12. # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]]
  13. t.sort()
  14. print t
  15. # [[1, 3, 4, 124], [2, 5, 146, 242], [3, 3, 7, 56]]
Nov 20 '07 #4

bartonc
Expert 5K+
P: 6,596
If zip()ing all the nth items together is what you are really after, then this will work:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = zip(a, b, c, r)
  7. print t
  8. # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)]
  9. for i, intTuple in enumerate(t):
  10.     t[i] = sorted(intTuple)
  11. print t
  12. # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]]
  13. t.sort()
  14. print t
  15. # [[1, 3, 4, 124], [2, 5, 146, 242], [3, 3, 7, 56]]
If you want to preserve the original lists, order them and sort them individually, you might do this:
Expand|Select|Wrap|Line Numbers
  1. a = [3, 2, 1]
  2. b = [7, 5, 3]
  3. c = [56, 242, 4]
  4. r = [3, 146, 124]
  5.  
  6. t = [a, b, c, r]
  7. print t
  8. # [[3, 2, 1], [7, 5, 3], [56, 242, 4], [3, 146, 124]]
  9. # notice that t is now a list of lists, to tuples #
  10. for intList in t:
  11.     intList.sort()
  12. print t
  13. # [[1, 2, 3], [3, 5, 7], [4, 56, 242], [3, 124, 146]]
  14. t.sort()
  15. print t
  16. # [[1, 2, 3], [3, 5, 7], [3, 124, 146], [4, 56, 242]]
Nov 20 '07 #5

bartonc
Expert 5K+
P: 6,596
here what i'm trying to do is ......
i want to sort the elements

a[o],b[0].c[0]

thats all ... i dont want to sort all the arrays ...
If I understand correctly, in order to sort the lists ("arrays") into their proper oder, you'll need to sort the individual lists as well.
Nov 20 '07 #6

bvdet
Expert Mod 2.5K+
P: 2,851
I am very new to python ... I am using windows xp

i am writing a program in python , in which i want to sort the first elements of say 5 arrays

eg :
Expand|Select|Wrap|Line Numbers
  1. a = [3,2,1]
  2. b=[7,5,3]
  3. c=[56,242,4]
  4. r=[3,146,124]
so i used zip to make them in to a single array
Expand|Select|Wrap|Line Numbers
  1. t= zip(a,b,c,d)
which = >
t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r(2))

now i used y=t(:1)
=> y = (a[0],b[0],c[0])


now if i say
y.sort()
print y


the result is y but its not gettin sorted
It looks like you are trying to do this:
Expand|Select|Wrap|Line Numbers
  1. a = [3,2,1]
  2. b = [7,5,3]
  3. c = [56,242,4]
  4. r = [3,146,124]
  5.  
  6. sort_list1 = [r,b,c,a]
  7. sort_list1.sort()
  8. print sort_list1
  9.  
  10.  
  11. sort_list2 = [a,b,c,r]
  12. sort_list2.sort()
  13. print sort_list2
Expand|Select|Wrap|Line Numbers
  1. >>> [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
  2. [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
  3. >>> 
Nov 20 '07 #7

P: 9
It looks like you are trying to do this:
Expand|Select|Wrap|Line Numbers
  1. a = [3,2,1]
  2. b = [7,5,3]
  3. c = [56,242,4]
  4. r = [3,146,124]
  5.  
  6. sort_list1 = [r,b,c,a]
  7. sort_list1.sort()
  8. print sort_list1
  9.  
  10.  
  11. sort_list2 = [a,b,c,r]
  12. sort_list2.sort()
  13. print sort_list2
Expand|Select|Wrap|Line Numbers
  1. >>> [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
  2. [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
  3. >>> 



in the above example if i have to print just the numbers of the first tuple
i.e

say in [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]

how do i print only [3,2,1 ]
or [3,146,124]

bcos i tried

for i in range(1):
print
Nov 20 '07 #8

bartonc
Expert 5K+
P: 6,596
in the above example if i have to print just the numbers of the first tuple
i.e

say in [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]

how do i print only [3,2,1 ]
or [3,146,124]

bcos i tried

for i in range(1):
print
Just like this:
Expand|Select|Wrap|Line Numbers
  1. print sort_list2[0]
  2. # or
  3. print sort_list2[1]
  4. # or
  5. for itme in sort_list2:
  6.     print item
Nov 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.