473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sorting

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
8 1075
bartonc
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
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
6,596 Expert 4TB
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

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.