Python inverse list

So, I tried to do this for a while and am constantly faced with various failures. I need to accept numerical input from the user and put it in a list and print it in a 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 it worked most of the time (I used numbers 2,3,4 and 10 as input, as I had problems displaying numbers in descending order for the first digit), but when I enter 16, 30, 24, it displays numbers as:

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

Here is another attempt:

 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 would be greatly appreciated as I am pretty new to python and struggling with my course.

-Callum

+7
python list
source share
4 answers

IN

 bids.append(bid) 

You will get a list of strings. You want to convert them to integers and sort them in descending 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 descending order.

In your code:

 for bid in bids: bid = int(bid) 

doing nothing. It converts each bid into a whole and immediately forgets.

and

 bids.sort([::-1]) 

It’s not about using the sort method. Read the docs for list.sort .

+8
source share

Your problem is fundamentally in this loop:

 for bid in bids: bid = int(bid) 

The above does not change the contents of bids . It creates new whole instances called bid every time with which you do nothing. Therefore, when you later move on to sorting bids , you just sort the original string elements, not the integers.

If you want to sort integer values, you have several options. One of them is to convert values ​​to integers when they enter:

 bids.append(int(bid)) 

One of them is converting and sorting in your print path:

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

Since the approach you seem to want to take is to replace bids string elements with whole 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)) 

Many people will tell you that the above is more succinctly written as

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

I used intbids as a separate list to make it clear what is going on. If you just want to throw away the line bet list as soon as you have an integer bet list, you can just use the same name:

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

If it is really really important to replace each element “in place” (this means that a new list is not being created), then the most simple way to understand is probably the index cycle:

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

(But until you are very experienced and work with tons and tons of data, my strong recommendation is not to worry about “updating lists in place.” It's not as natural as writing Python.)

+3
source share

This will sort the list of bets as numerical strings, in reverse order, numerical order:

 bids.sort(key=int, reverse=True) 

And if you need to print it vertically:

 print('\n'.join(bids)) 
+2
source share

Ok, let me explain: You are not sorting your data!

In your second case, your list is [16,30,24] , and you print it in the reverse order of 24, 30, 16 , which is correct.

I see two problems:
- you do not sort print values
- you do not check whether the new bid is higher than the previous one (what type of bidding?)

For the first part, when the list already contains an int , you just need to sort it:

 bids.sort() 

Or if you want it in reverse order:

 bids.sort(reverse=True) 

For the second part, just check if there is more new entry before adding it to the list:

 while bid != '': if bids and bid < bids[-1]: print "Error: your bid must be higher than last bid. Last bid: {}".format(bids[-1]) else: bids.append(bid) bid = input('Bid: ') 
+1
source share

All Articles