Python 非基类型集合?

发布于 2024-10-12 10:19:41 字数 396 浏览 2 评论 0原文

我正在学习 python,我对列表、元组、字典、集合等不同数据结构的用途有点困惑。

例如,假设汽车是由品牌、型号、马力定义的
我希望有一个汽车集合,我可以

  • 按马力进行排序,或
  • 按型号进行品牌比较(以删除重复项),
  • 只需使用 for 语句进行迭代即可
  • 按型号删除元素

实现此目的的最佳方法是什么?

  • 我是否需要创建一个 Car 类,并重新定义一些函数(如 C 中的运算符 == 和 >),然后将它们存储在列表中
  • 或者我应该将它们制作为字典,还是自己重新定义字典?然后让 python 为我对它们进行排序(我认为这对于操作员模块是可能的,如果我错了请纠正我)
  • 还有其他吗?

I'm learning python, and I'm a bit confused about which the purpose of the different data structures like list, tuples, dictionaries, sets.

For example, let's say cars are defined by Brand,Model,Horsepower
I wish to have a collection of cars on which I could

  • sort by Horsepower, or brand
  • compare by model (to erase duplicates)
  • iterate simply with a for statement
  • remove elements by model

What would be the best way to achieve this?

  • Do I need to create a class Car, and redefine some functions (like operators == and > in C), then store them in a list
  • Or should I make them a dictionary, or redefine a dictionary myself ? and then have python sort them for me (I think this is possible with the operator module, correct me if I'm wrong)
  • something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人接听 2024-10-19 10:19:41

像大多数事情一样,有不止一种可能的方法。

使用类

最巧妙的可能是创建一个 Car 类。您不需要重新定义运算符——只需一些属性和构造函数。

class Car:
    def __init__(self, brand, model, hp):
        self.brand = brand
        self.model = model
        self.hp = hp

然后,如果您想按型号进行比较:

car1 = Car('foo', 'bar', 23)
car2 = Car('baz', 'bar', 34)

if car1.model == car2.model:
    # models are equal

如果您有一个 Car 对象列表,并想按马力对它们进行排序:

car1 = Car('foo', 'bar', 23)
car2 = Car('baz', 'bar', 34)
car3 = Car('baz', 'bar', 14)
car_list = [car1, car2, car3]

car_list.sort(key=lambda c: c.hp)

使用字典

当然,因为您将对该类执行所有操作正在存储一组属性,您也可以只使用字典来代替 - 您只是失去了刚性(并获得了一些灵活性)。模仿上面的代码,但使用字典:

{'brand': 'foo', 'model': 'bar', 'hp': 23}

然后,如果您想按型号进行比较:

car1 = {'brand': 'foo', 'model': 'bar', 'hp': 23}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 34}

if car1['model'] == car2['model']:
    # models are equal

如果您有一个 Car 对象列表,并且想按马力对它们进行排序:

car1 = {'brand': 'foo', 'model': 'bar', 'hp': 23}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 34}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 14}
car_list = [car1, car2, car3]

car_list.sort(key=lambda c: c['hp'])

Like most things, there's more than one possible approach.

Using a class

The neatest would probably be to make a Car class. You don't need to redefine operators - just have some properties and a constructor.

class Car:
    def __init__(self, brand, model, hp):
        self.brand = brand
        self.model = model
        self.hp = hp

Then, if you wanted to compare by model:

car1 = Car('foo', 'bar', 23)
car2 = Car('baz', 'bar', 34)

if car1.model == car2.model:
    # models are equal

If you had a list of Car objects, and wanted to sort them by horsepower:

car1 = Car('foo', 'bar', 23)
car2 = Car('baz', 'bar', 34)
car3 = Car('baz', 'bar', 14)
car_list = [car1, car2, car3]

car_list.sort(key=lambda c: c.hp)

Using a dict

Of course, since all you'd be doing with the class is storing a set of properties, you could also just use dicts instead - you just lose the rigidity (and gain some flexibility). To mimic the code above, but with dicts:

{'brand': 'foo', 'model': 'bar', 'hp': 23}

Then, if you wanted to compare by model:

car1 = {'brand': 'foo', 'model': 'bar', 'hp': 23}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 34}

if car1['model'] == car2['model']:
    # models are equal

If you had a list of Car objects, and wanted to sort them by horsepower:

car1 = {'brand': 'foo', 'model': 'bar', 'hp': 23}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 34}
car2 = {'brand': 'baz', 'model': 'bar', 'hp': 14}
car_list = [car1, car2, car3]

car_list.sort(key=lambda c: c['hp'])
马蹄踏│碎落叶 2024-10-19 10:19:41

我会用一个类 Car 和一个 dict(model:Car) 来存储不同的汽车。

I would do it with a class Car and a dict(model:Car) store different cars.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文