Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2005, 5:02 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Sorting lists

def sort_list(group):
   ##sorted_list = ... ...
   return sorted_list


import random
a = random.randrange(1, 6)
b = random.randrange(1, 6)
c = random.randrange(1, 6)
d = random.randrange(1, 6)
e = random.randrange(1, 6)

sorted_list = sort_list([a, b, c, d, e])
print sorted_list

What do I replace the bold with to make the function return a sorted list (greatest to least)? Is there a simple command I'm forgetting?
Sane is online now   Reply With Quote
Old May 2nd, 2005, 5:42 PM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
yes, your forgetting the sort() method. for example:
x=[1,4,2,9]
x.sort()
print x   #prints [1, 2, 4, 9]
x.reverse()
print x  #prints [9,4,2,1], if you want your list in descending order
ZenMasterJG is offline   Reply With Quote
Old May 2nd, 2005, 5:59 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Wow dude, thanks!

And while I'm at it, I was trying to make a sort function. I ran into the problem of deleting certain items from a list. I know you can delete the last item, but can you delete a specific item from a list?
Sane is online now   Reply With Quote
Old May 2nd, 2005, 6:07 PM   #4
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
Yeah.
if you want to remove by a value, then you can do somthing like this:
x=['spam','ham','eggs','bacon']
x.remove('ham')
or, if you want to remove by positions, you can use slices (ahh, i love slices, they're the bestest)
[code]
x=['spam','ham','eggs','bacon']
x=x[:2]
print x #prints ['spam', 'ham']
[code]
ZenMasterJG is offline   Reply With Quote
Old May 2nd, 2005, 6:32 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
That's awesome! Thanks.
Sane is online now   Reply With Quote
Old May 2nd, 2005, 7:22 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
This is really weird...

def return_turns(party, enemy):    
    SpeedSet = []
    Set = []
    for a in range(len(party)):
        Set.append(str(party[a][1])+"."+str(party[a][0]))
        b= float(Set[a])
        print b
        SpeedSet.append(b)
        
    for a in range(len(enemy)):
        SpeedSet.append(float(enemy[a][1])+ float(str(float(enemy[a][0])/10)[:3]))
    SpeedSet.sort()
    SpeedSet.reverse()
    NewSet = []

    print SpeedSet
                              
    #for a in range(len(SpeedSet)):
    #    NewSet.append([(max(SpeedSet)-SpeedSet[a][0])/5+1, SpeedSet[a][1][0]])
    #
    #turns = []
    #while len(turns)<100:
    #    for a in range(len(NewSet)):
    #        if a%NewSet[a] == 0:
    #            turns.append(NewSet[a][1])
    #return SpeedSet

player1 = [1, 15]
player2 = [2, 18]
enemy1 = [3, 8]
enemy2 = [4, 4]
party = [player1, player2]
enemy = [enemy1, enemy2]

turns = return_turns(party, enemy)
print turns #turns[len(turns)-1]

Technically, the bold lines should output the same results, but one has like 10 decimal places for no apparent reason. :s!!!
Sane is online now   Reply With Quote
Old May 2nd, 2005, 7:33 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Floating-point numbers are printed to 10 significant figures (or something) by default.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 2nd, 2005, 7:59 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
How come b isn't then? It's a float.
Sane is online now   Reply With Quote
Old May 2nd, 2005, 8:08 PM   #9
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
If you're using python 2.4:
mylist = [random.randrange(1,6) for i in range(5)]
mysortedlist = reversed(sorted(mylist))
print mysortedlist

--OH.
hydroxide is offline   Reply With Quote
Old May 2nd, 2005, 8:31 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
It's usually a good idea to read all the posts before making yours.
Sane is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC