python - remove all values from multiple lists at same index position with some logic -
i'm beginner in programming , beginner in python. below details better understanding of issue.
rooms = ['blueroom', 'redroom', 'greenroom', 'blueroom'] availability = [5, 7, 3, 5] description = ['blue', 'red', 'green', 'blue'] price = [120, 90, 150, 115]
what need have is, same lists, blueroom , of values in same index position of lists highest price removed, this:
rooms = ['redroom', 'greenroom', 'blueroom'] availability = [7, 3, 5] description = ['red', 'green', 'blue'] price = [90, 150, 115]
i appreciate if me. lists example, lists have handle have several rooms duplicated , have same each one.
edit: first of all, thank quick answers. on other hand, forgot mention little, not less important @ all, detail. have code using old interpreter, jython version 2.2. so, functions may not work me.
as jochen pointed out in comment, you're better off writing class store information. each instance of class can store information relating single room.
class room(object): def __init__(self, name, availability, description, price): self.name = name self.availability = availability self.description = description self.price = price rooms = [room('blueroom', 5, 'blue', 120), ...]
this way, information single room kept together, , don't have worry keeping different lists in sync. can search list find duplicate rooms , remove ones don't want.
for example, suppose want keep lowest-priced room matching given description. way i'd is
import itertools # here might insert class code above def namekey(room): return room.name def pricekey(room): return room.price sorted_rooms = sorted(rooms, namekey)
first, need sort list rooms name, in order following code work.
cheapest_rooms = []
that creates new list we'll fill cheapest room each name.
for name, group in itertools.groupby(sorted_rooms, namekey):
this split list of rooms sub-lists name.
cheapest = min(group, key=pricekey)
this finds cheapest room out of group.
cheapest_rooms.append(cheapest)
and appends list.
all can condensed into
import itertools # here might insert class code above def namekey(room): return room.name def pricekey(room): return room.price [min(group, key=pricekey) name, group \ in itertools.groupby(sorted(rooms, namekey), namekey)]
Comments
Post a Comment