LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2014, 08:59 AM   #1
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Rep: Reputation: Disabled
Question python sorting


have written a python code to get the following result

Expected result
basically sorting from right


Code:
[8 ,11 ,4 ,33][]
[8 11 4 ][33]
[8 11 ][4,33]
[8][4,11,33]
[][4,8,11,33]
i wrote a python code but i think am missing something

Code:
def insert_switch(list):
    for i in range(len(list)-1,0) :
value =list[i]       
        for j in range(i,len(list)) :
            if (list[j-1]>list[j]) :
                value=list[j]
                list[j]=list[j-1]
                list[j-1]=value
                j=j+1
                i=i-1
else :
break

Last edited by wackysiya; 02-23-2014 at 11:29 AM.
 
Old 02-23-2014, 09:28 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,601

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I think your "value =list[i]" should be indented to the same level as the "for j ...".

And the "else :" should be at the same level as the "if (list[j-1]...", with the break indented under that.
 
Old 02-23-2014, 10:31 AM   #3
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Question

so should i not have the break line?

i didnt quite understand
 
Old 02-23-2014, 10:49 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,601

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Indentation is critical in Python...

Code blocks are all indented to the same level. Since the value =list[i] breaks indentdation, I would think it would also break the block containing the nested loops. The same would go for the Else and the break.
 
Old 02-23-2014, 08:19 PM   #5
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS
Posts: 100

Rep: Reputation: 1
Have you been tasked with creating your own sort?

I would use the sorted() function.

Code:
a = [5,3,4,1,2]

print sorted(a)
This is just a really simple example, but I think you get the idea.
 
Old 06-22-2014, 05:08 PM   #6
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python - Sorting Lists inside of Lists LXer Syndicated Linux News 0 06-06-2013 02:00 PM
sorting & finding unique in python logs kshabbir Linux - General 4 03-08-2012 03:05 AM
[SOLVED] list sorting in python aihaike Programming 4 05-12-2011 01:20 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 09:30 PM


All times are GMT -5. The time now is 08:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration