0

example

Class car:
   def car(color, price, speed):
     self.color = color
     self.price = price
     self.speed = speed

A list car_list is generated with 10 instances of car

car_list = [car1,car2,car3,car4,car5,car6,car7,car8,car9,car10,]

Each car object has a random color, price and speed value.

I want to create a function that checks if 2 or more cars have the same color for example. If yes, create a list with only those cars.

red_cars=[car4,car7,car10]

A lot of googling didn't help me get started.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
  • https://stackoverflow.com/questions/9835762/how-do-i-find-the-duplicates-in-a-list-and-create-another-list-with-them Maybe this will help – RishabhHardas Mar 18 '19 at 11:12
  • Possible duplicate of [How do I find the duplicates in a list and create another list with them?](https://stackoverflow.com/questions/9835762/how-do-i-find-the-duplicates-in-a-list-and-create-another-list-with-them) – Daniele Santi Mar 18 '19 at 11:15
  • As @shotgunner said in his answer, your class definition is wrong. How do you create the instances with random values? (also, class without capital letter). – Valentino Mar 18 '19 at 12:28

3 Answers3

2

Your defintion of car class is wrong. you don't create constructor __init__ for it:

Class car:
    def __init__(self, color, price, speed):
        self.color = color
        self.price = price
        self.speed = speed

def is_red(car):
    return car.color=="red"

# creating car1, car2, .... , car10 objects here
car_list = [car1,car2,car3,car4,car5,car6,car7,car8,car9,car10,]

you can use filter function to filter objects in a list your list:

>>> print(filter(is_red, car_list))
# [...list of car objects that their color is red]
Mojtaba Kamyabi
  • 3,440
  • 3
  • 29
  • 50
0

You can use a list comprehension.

If car_list is the full list, and you want the red cars, you could do:

red_cars = [c for c in car_list if c.color == "red"]

No need of custom methods to check the attribute values with this method.
However as @shotgunner said, you should fix your class definition. It's not like C++: in python the constructor is called __init__ and the first argument must be self. Here more info on classes in python.

Valentino
  • 7,291
  • 6
  • 18
  • 34
0

You can use this code:

car_list = [car1, car2, car3, car4, car5, car6, car7, car8, car9, car10]
dd = {}

for i in range(len(car_list)):
    for j in range(len(car_list)):
        if i != j:
            if car_list[i].color == car_list[j].color:
                if car_list[i].color+'_cars' in dd:
                    dd[car_list[i].color+'_cars'] = dd[car_list[i].color+'_cars']
                else:
                    dd[car_list[i].color+'_cars'] = set()

                dd[car_list[i].color+'_cars'].add(car_list[i])
                dd[car_list[i].color+'_cars'].add(car_list[j])
print(dd)

Instead of creating list, I have created sets so that it will trim out the duplicate results (generated from my code) itself. And for the variable part, both the answers above have used static value (i.e. red) for conditioning as well as for the variable creation. So what I have done is, created a dictionary so that dynamic assignment of key for each color is possible (which is what I think the author wants).

Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33