gpt4 book ai didi

python - 在复合对象中搜索字段

转载 作者:行者123 更新时间:2023-11-28 22:44:23 24 4
gpt4 key购买 nike

我是 Python 的新手,正在创建一个程序来跟踪我学区学生使用的 RFID 徽章。在我的程序中,我有一个名为 Badge 的类,它创建一个带有六个公共(public)命名变量(RFID、studentID、status、dateActivated、dateDeactivated、reason)的徽章对象。然后我有一个名为 BadgeTable 的类,其中包含徽章。

我想做的是查看任何徽章中是否存在特定的 RFID 或学生 ID。我知道我可以用循环来做到这一点,但这可能需要一段时间(我正在阅读大约 450 个 CSV 文件,每个文件包含 2000 到 14000 个徽章,并且只有在尚未添加徽章时才向 BadgeTable 添加徽章).

Edit: to expand on the above, all badge data for the last three years has been manually entered into a spreadsheet that has logic to identify duplicate RFID or studentID numbers, then exported each day as a CSV file for the badge reader software. For the first year of our bus badge program each day's CSV file only contained active badges; we naively assumed that if we stopped sending inactive badges they would be rejected by the badge reader. When we discovered we were wrong, we started including the inactive badges we knew about in the CSV files (we added a suffix to the studentID for each inactive badge to keep them unique), so from then on each days's CSV file was a superset of the previous day's file. However, there are a not insignificant number inactive/lost badges not in the current CSV file, so the above process was meant to be a way use the old CSV files to a) get as complete a list as possible of badges we've issued, and b) eliminate misspellings in the student names associated with each badge.

我希望能够说出类似的话:

studentBadges = BadgeTable()
#
# Bunch of code to load badges into studentBadges
#
if RFID not in studentBadges[:].RFID:
studentBadges.add_badge(RFID, studentID, 'A', fileDate, None, None)

我想要做的是查看变量 RFID 是否与 studentBadges 中任何徽章中的 RFID 字段相匹配。我试过了,还有 studentBadges[:][1],但都失败了(都因为 'BadgeTable' object is not subscriptable)

我目前的解决方法是在名为 RFIDList 的 BadgeTable 类中实际存储一个单独的列表,当每个徽章被添加到 BadgeTable 时,我还将它的 RFID 值添加到 RFIDList。只要我从不对 RFIDList 或徽章列表本身进行排序,我就可以执行以下操作:

def delete_badge(self, RFID):
"""Delete an individual badge from BadgeTable """
if RFID in self.RFIDList:
idx = self.RFIDList.index(RFID)
del self.RFIDList[idx]
del self.BadgeTable[idx]

是否有一种更优雅、更符合 Python 风格的方法来测试 Python 3 中复合对象中值的存在,而不涉及在类中保留并行列表?

最佳答案

我不太确定为什么需要 BadgeTable 成为一个类,而不只是让 studentBadges 成为一个 collection 徽章。它会做一些额外的事情来保存数据吗?

无论如何,了解 python 必须提供列表之外的哪些集合是有意义的。您似乎关心 Badge 的唯一性,但不关心它们的顺序。列表(类似于其他编程语言中的“数组”)都是关于顺序的。 Setsdictionaries (其他语言中的“关联数组”或“ HashMap ”)是无序的,但可以帮助您实现唯一性:

  • 集合最多包含每个对象一次
  • 字典最多包含每个键对象一次。每个键都指向一个值对象。 (多个键可能指向相同的值。)

所以你可以有两本字典,一本将 RFID 映射到 Badge,一本将学生 ID 映射到 Badge。在其中查找比在列表中查找要快得多。

但更好的方法可能是将 Badge 放在一个集合中。第二次尝试添加 Badge 将无济于事。持有一组(数学)唯一对象正是(python)集合所做的。

但是什么时候两个对象“相同”? Badge 等用户定义类的实例是 hashable默认情况下,但比较不等于除了自己。因此,您根据读入数据新创建的每个 Badge 都将被视为与所有其他的不同,即使某些共享其 RFIDstudentID。因此,覆盖 Badge 中的方法 __hash__()__eq__(),以便 Badge 比较相等,当且仅当它们 RFID匹配。

注意事项:平等一致性

平等应该是可传递的。 (即如果 a == bb == c,那么 a == c。)因此你可以制作 Badge 比较相等,如果他们的RFID他们的studientID匹配,但你不应该让他们已经相等,如果有的话,他们的RFID他们的studientID匹配。 (无论如何,不​​可能找到与后者一致的有意义的 __hash__() 实现。)

虽然我猜想如果一个学生在他或她的学校生涯中获得了多个徽章(带有不同的 RFID),无论如何,您的程序可能想要了解的不仅仅是第一个徽章。

6'300'000 个徽章

看看您在这里处理的数据量,您确定要(并且能够)将其全部保存在内存中吗?如果没有,使用 python 访问您选择的数据库管理系统可能是可行的方法。关系数据库还支持您希望依赖的集合操作语义。

关于python - 在复合对象中搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com