重载列表理解行为?

发布于 2024-09-16 04:16:01 字数 745 浏览 1 评论 0原文

我的任务是创建一个硬件笼模型。每个笼子包含 N 个插槽,每个插槽可能包含也可能不包含卡片。

我想使用列表来模拟笼子。每个列表索引将对应于槽号。 cards[0].name="Card 0" 等。

这将允许我的用户通过简单的列表理解来查询模型。例如:

for card in cards:
    print card.name

我的用户不是熟练的 Python 用户,他们将与模型实时交互,因此让列表索引不对应于填充的卡片是不切实际的。换句话说,如果用户移除一张卡,我需要执行一些操作来指示该卡未填充 - 我的第一反应是将列表项设置为 None

老板喜欢这个方案,但他并不热衷于上面的列表理解如果缺少一张卡片就会失败。 (目前确实如此。)他甚至不太支持要求用户学习足够的 Python 来创建忽略 None 的列表理解表达式。

我的想法是对 list 类进行子类化,以创建一个 newclass。它的工作方式与列表完全相同,但 for card in cards 只会返回未设置为 None 的成员。

有人可以演示如何重载列表类,以便子类上调用的列表推导式将忽略 None 吗? (到目前为止,当我尝试这样做时,我的 Python 技能已经开始下降。)

任何人都可以提出更好的方法吗?

I'm tasked with creating a model of a cage of hardware. Each cage contains N slots, each slot may or may not contain a card.

I would like to model the cage using a list. Each list index would correspond to the slot number. cards[0].name="Card 0", etc.

This would allow my users to query the model via simple list comprehensions. For example:

for card in cards:
    print card.name

My users, which are not sophisticated Python users, will be interacting with the model in real-time, so it is not practical to have the list index not correspond to a populated card. In other words, if the user removes a card, I need to do something that will indicate that the card is not populated—my first impulse was to set the list item to None.

The Bossman likes this scheme, but he's not crazy about the list comprehension above failing if there is a card missing. (Which it currently does.) He's even less supportive of requiring the users to learn enough Python to create list comprehension expressions that will ignore None.

My thought was to sub-class the list class, to create a newclass. It would work exactly like a list, except for card in cards would only return members not set to None.

Will someone please demonstrate how to overload the list class so that list comprehensions called on the subclass will ignore None? (My Python skills have so far begun to break down when I attempt this.)

Can anyone suggest a better approach?

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

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

发布评论

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

评论(4

无法回应 2024-09-23 04:16:02

您可以为此提供一个生成器/迭代器。

def installed(cage):
    for card in cage:
        if card:
            yield card

cards = ["Adaptec RAID", "Intel RAID", None, "Illudium Q-36 Explosive Space Modulator"]

# print list of cards
for card in installed(cards):
    print card

You could provide a generator/iterator for this.

def installed(cage):
    for card in cage:
        if card:
            yield card

cards = ["Adaptec RAID", "Intel RAID", None, "Illudium Q-36 Explosive Space Modulator"]

# print list of cards
for card in installed(cards):
    print card
冷情 2024-09-23 04:16:02

如果您使用的是 2.6 或更高版本,您可以执行类似的操作来获取名称:

names = [x.name for x in cards if x is not None]

我认为这应该接近您想要的。

You can do something like this to get the names if you're using 2.6 or newer:

names = [x.name for x in cards if x is not None]

That should get close to what you're after I think.

你的背包 2024-09-23 04:16:02

也许定义一个函数(假设cards是一个全局变量?!?):

def pcards():
    for card in cards:
        if card:
            print card.name

这样你的用户就可以简单地输入pcards()来获取列表。

Perhaps define a function (assuming cards is a global variable?!?):

def pcards():
    for card in cards:
        if card:
            print card.name

so your users can simply type pcards() to get a listing.

不羁少年 2024-09-23 04:16:01
>>> class MyList(list):
...     def __iter__(self):
...         return (x for x in list.__iter__(self) if x is not None)
... 
>>> 
>>> ml = MyList(["cat", "dog", None, "fox"])
>>> for item in ml:
...     print item
... 
cat
dog
fox

>>> [x for x in ml]
['cat', 'dog', 'fox']
>>> list(ml)
['cat', 'dog', 'fox']
>>> class MyList(list):
...     def __iter__(self):
...         return (x for x in list.__iter__(self) if x is not None)
... 
>>> 
>>> ml = MyList(["cat", "dog", None, "fox"])
>>> for item in ml:
...     print item
... 
cat
dog
fox

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