443,818 Members | 1,223 Online
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 a = [3,2,1] b=[7,5,3] c=[56,242,4] r=[3,146,124] so i used zip to make them in to a single array Expand|Select|Wrap|Line Numbers 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 Replies

 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 a = [3,2,1] b=[7,5,3] c=[56,242,4] r=[3,146,124] so i used zip to make them in to a single array Expand|Select|Wrap|Line Numbers 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 a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = a + b + c + r print t # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124] t.sort() print t # [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 a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = a + b + c + r print t # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124] t.sort() print t # [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

 Expert 5K+ P: 6,596 I didn't try zip(). I simply add the lists together: Expand|Select|Wrap|Line Numbers a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = a + b + c + r print t # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124] t.sort() print t # [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 a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = zip(a, b, c, r) print t # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)] for i, intTuple in enumerate(t):     t[i] = sorted(intTuple) print t # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]] t.sort() print t # [[1, 3, 4, 124], [2, 5, 146, 242], [3, 3, 7, 56]] Nov 20 '07 #4

 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 a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = zip(a, b, c, r) print t # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)] for i, intTuple in enumerate(t):     t[i] = sorted(intTuple) print t # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]] t.sort() print t # [[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 a = [3, 2, 1] b = [7, 5, 3] c = [56, 242, 4] r = [3, 146, 124]   t = [a, b, c, r] print t # [[3, 2, 1], [7, 5, 3], [56, 242, 4], [3, 146, 124]] # notice that t is now a list of lists, to tuples # for intList in t:     intList.sort() print t # [[1, 2, 3], [3, 5, 7], [4, 56, 242], [3, 124, 146]] t.sort() print t # [[1, 2, 3], [3, 5, 7], [3, 124, 146], [4, 56, 242]] Nov 20 '07 #5

 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

 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 a = [3,2,1] b=[7,5,3] c=[56,242,4] r=[3,146,124] so i used zip to make them in to a single array Expand|Select|Wrap|Line Numbers 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 a = [3,2,1] b = [7,5,3] c = [56,242,4] r = [3,146,124]   sort_list1 = [r,b,c,a] sort_list1.sort() print sort_list1     sort_list2 = [a,b,c,r] sort_list2.sort() print sort_list2 Expand|Select|Wrap|Line Numbers >>> [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]] [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]] >>>  Nov 20 '07 #7

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

 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 print sort_list2[0] # or print sort_list2[1] # or for itme in sort_list2:     print item Nov 20 '07 #9