Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing lists of numbers in python

Tags:

python

list

So I have been trying to do this for a while now and am constantly coming up with differing failures. I need to take numerical input from the user and put it into a list and output it in decreasing value:

bids = []
bid = input('Bid: ')
while bid != '':
  bids.append(bid)
  bid = input('Bid: ')
print('The auction has finished! The bids were:')

for bid in bids:
  bid = int(bid)

for bid in reversed(bids):
 print(bid)

So this worked well most of the time, (I have been using the numbers 2,3,4 & 10 as input as I have been having problems where it shows the numbers in decreasing order for the first numeral) but when I type in 16, 30, 24 it displays the numbers as:

The auction has finished! The bids were:
24
30
16

Here is another version I have attempted:

bids = []
bid = input('Bid: ')
while bid != '':
  bids.append(bid)
  bid = input('Bid: ')
print('The auction has finished! The bids were:')

for bid in bids:
  bid = int(bid)

bids.sort([::-1]) #Here is where the problem is at, I don't know
                  #what the correct syntax is for something like that

for bid in bids:
  print(bid)

Any help will be much appreciated as I am fairly new to python and am struggling with my course.

-Callum

like image 264
C Pastuszak Avatar asked Dec 28 '15 14:12

C Pastuszak


2 Answers

In your

bids.append(bid)

you get a list of strings. You want to convert them to integers and sort in decreasing order:

numerical_bids = [int(bid) for bid in bids]
numerical_bids.sort(reverse=True)

and now numerical_bids is a list of integers sorted in decreasing order.

In your code:

for bid in bids:
    bid = int(bid)

does nothing. It converts each bid to an integer and forgets it immediately.

And

bids.sort([::-1])

is not how to use the sort method. Read the docs for list.sort.

like image 110
eumiro Avatar answered Oct 19 '22 04:10

eumiro


Your problem is fundamentally in this loop:

for bid in bids:
  bid = int(bid)

The above does not alter the contents of bids. It creates new integer instances, named bid each time, which you don't do anything with. So when you later go to sort bids, you are just sorting the original string elements, not your desired integers.

If you want to sort the integer values, you have a few choices. One is to convert the values to integers as they come in:

bids.append(int(bid))

One is to convert and sort right in your print loop:

for bid in sorted((int(x) for x in bids), reverse=True):
  print(bid)

Since the approach you seemed to want to take is to replace the string elements of bids with integer elements instead, you probably want to do something like your original loop, but don't throw away the results:

intbids = []
for bid in bids:
    intbids.append(int(bid))

Lots of folks will tell you that the above is more concisely written as

intbids = [int(bid) for bid in bids]

I used intbids as a separate list to make it clear what is happening. If you want to just throw away the string bid list once you have the integer bid list, you can just use the same name:

bids = [int(bid) for bid in bids]

If it's really, really important to do the replacement of each element "in place" (meaning don't create a new list), then the simplest-to-understand way is probably to loop over the indices:

for i in range(len(bids):
    bids[i] = int(bids[i])

(But until you are very experienced and working with tons and tons of data, my strong recommendation is to not worry about "in-place" updating of lists. It's not as natural a way to write Python.)

like image 40
John Y Avatar answered Oct 19 '22 03:10

John Y