📜  在Python中搜索对象列表

📅  最后修改于: 2022-05-13 01:54:47.644000             🧑  作者: Mango

在Python中搜索对象列表

在本文中,我们将讨论如何在Python中搜索对象列表。

可以通过遍历列表来搜索特定或一组对象。

语法

class_name.object_name

在哪里,

  • class_name 是类的名称
  • object_name 是对象的名称

示例 1:

创建具有以下属性的 Car 类并执行搜索操作,返回价格低于100 万 (10,00,000/-) 的汽车。

属性:

  • 弦乐公司
  • 字符串模型名称
  • 国际价格
  • 座位容量
Python3
class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# cars with price less than 10 Lakhs
economicalCars = [car for car in carsList if car.price <= 1000000]
 
# print those cars
for car in economicalCars:
    print(car.company+'--'+car.modelName)


Python3
class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# cars having seating capacity 4
smallCars = [car for car in carsList if car.seatingCapacity == 4]
 
# print those cars
for car in smallCars:
    print(car.company+'--'+car.modelName+'--'+str(car.seatingCapacity))


Python3
class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# bmw cars
BMW_Cars = [car for car in carsList if car.company == 'BMW']
 
# print those cars
for car in BMW_Cars:
    print(car.company+'--'+car.modelName+'--' +
          str(car.price)+'--'+str(car.seatingCapacity))


输出
Honda--Jazz
Suzuki--Alto

示例 2

使用相同的汽车类别搜索座位容量为 4 的汽车。

Python3

class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# cars having seating capacity 4
smallCars = [car for car in carsList if car.seatingCapacity == 4]
 
# print those cars
for car in smallCars:
    print(car.company+'--'+car.modelName+'--'+str(car.seatingCapacity))
输出
Suzuki--Alto--4

示例 3:

使用上述相同的Car 类,搜索BMW公司的汽车并返回它们。

Python3

class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# bmw cars
BMW_Cars = [car for car in carsList if car.company == 'BMW']
 
# print those cars
for car in BMW_Cars:
    print(car.company+'--'+car.modelName+'--' +
          str(car.price)+'--'+str(car.seatingCapacity))
输出
BMW--X5--9000000--5