I’m a beginner in programming and also a beginner in Python. Below are the details for a better understanding of my issue.
Rooms = ['BlueRoom', 'RedRoom', 'GreenRoom', 'BlueRoom']
Availability = [5, 7, 3, 5]
Description = ['Blue', 'Red', 'Green', 'Blue']
Price = [120, 90, 150, 115]
What I need to have is, the same lists, but with the BlueRoom and all of the values in the same index position of the lists with the highest price removed, like this:
Rooms = ['RedRoom', 'GreenRoom', 'BlueRoom']
Availability = [7, 3, 5]
Description = ['Red', 'Green', 'Blue']
Price = [90, 150, 115]
I would really appreciate if someone could help me. This lists are just for example, but the lists I have to handle will have several rooms that are duplicated and I will have to do the same with each one.
EDIT: First of all, thank you all for the quick answers. On the other hand, I forgot to mention a little, but not less important at all, detail. I have to code using a very old interpreter, Jython version 2.2. So, some functions may not work for me.
As Jochen pointed out in a comment, you’re probably better off writing a class to store your information. Each instance of a class can store all the information relating to a single room.
This way, all the information about a single room is always kept together, and you don’t have to worry about keeping different lists in sync. So you can just search the list to find the duplicate rooms and remove the ones you don’t want.
For example, suppose you want to only keep the lowest-priced room matching a given description. The way I’d do that is
First, you will need to sort the list rooms by name, in order for to the following code to work.
That creates a new list that we’ll fill with the cheapest room with each name.
This will split the list of rooms into sub-lists by name.
This finds the cheapest room out of the group.
And that appends it to the list.
All this can be condensed into