![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
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? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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? |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Yeah.
if you want to remove by a value, then you can do somthing like this: x=['spam','ham','eggs','bacon']
x.remove('ham')[code] x=['spam','ham','eggs','bacon'] x=x[:2] print x #prints ['spam', 'ham'] [code] |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
That's awesome!
Thanks. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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!!! |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Floating-point numbers are printed to 10 significant figures (or something) by default.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
How come b isn't then? It's a float.
![]() |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
If you're using python 2.4:
mylist = [random.randrange(1,6) for i in range(5)] mysortedlist = reversed(sorted(mylist)) print mysortedlist --OH. |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
It's usually a good idea to read all the posts before making yours.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|