简体   繁体   English

Python 2.6:如何有效地在一个特定字段上比较两个相同对象类型的列表?

[英]Python 2.6: How can I compare two lists of same object types on one particular field, efficiently?

I have a class called "UserDatabaseRecord". 我有一个名为“UserDatabaseRecord”的类。 It has a bunch of fields like "username", "expiration_date", and so on. 它有一堆字段,如“username”,“expiration_date”等。

I have TWO lists of UserDatabaseRecord objects: List A and List B 我有两个UserDatabaseRecord对象列表:列表A和列表B.

I want to verify that for all UserDatabaseRecords in List A, the username field does not match any UserDatabaseRecords username field in List B. 我想验证对于列表A中的所有UserDatabaseRecords,用户名字段与列表B中的任何UserDatabaseRecords用户名字段都不匹配。

I'm able to accomplish this very inefficiently: 我能够非常低效地完成这个任务:

for record_a in List_A:
   for record_b in List_B:
      if record_a.username == record_b.username:
         print "Duplicate username: {0}".format(record_a.username)

It works, I guess. 我觉得它很有用。 I'd just like to make it more efficient and/or "Pythonic". 我只是想让它更高效和/或“Pythonic”。

This question is related, but ultimately I couldn't figure out how to apply it to a List of objects when comparing on just one field: one-liner to check if at least one item in list exists in another list? 这个问题是相关的,但最终我无法弄清楚如何只在一个字段上进行比较时如何将它应用于对象列表: 单行检查是否列表中至少有一个项目存在于另一个列表中?

The problem with this is that, for each element in list A, you're checking all of the elements in list B. So, if the lengths of your lists are N and M, that's N*M comparisons. 这个问题是,对于列表A中的每个元素,你要检查列表B中的所有元素。因此,如果列表的长度是N和M,那就是N * M比较。

If you make a set of the usernames from list B, then you can just use the in operator on it—which is not only simpler, it's instantaneous, instead of having to check all of the values one by one. 如果从列表B中创建一组用户名,那么您可以在其上使用in运算符 - 这不仅更简单,而且是瞬时的,而不是必须逐个检查所有值。 So, you only need N lookups instead of N*M. 因此,您只需要N次查找而不是N * M.

So: 所以:

b_names = {record.username for record in List_B}
for record_a in List_A:
    if record_a.username in b_names:
        print "Duplicate username: {0}".format(record_a.username)

Or, even simpler, use set intersection: 或者,甚至更简单,使用集合交集:

a_names = {record.username for record in List_A}
b_names = {record.username for record in List_B}
for name in a_names & b_names:
    print "Duplicate username: {0}".format(name)

And really, you don't need both of them to be sets, you can make one a set and the other just an iterator, using a generator expression: 实际上,你不需要它们都是集合,你可以使用生成器表达式创建一个集合而另一个只是一个迭代器:

a_names = {record.username for record in List_A}
b_names = (record.username for record in List_B)
for name in a_names.intersection(b_names):
    print "Duplicate username: {0}".format(name)

One of these may be a little faster than the others, but they'll all be in the same ballpark—and, more importantly, they're all linear instead of quadratic. 其中一个可能比其他一个快一点,但它们都在同一个球场 - 更重要的是,它们都是线性的而不是二次的。 So, I'd suggest using whichever one makes the most sense to you. 所以,我建议使用对你来说最有意义的一个。

If you just need to know whether there are duplicates rather than get a list of them, or just need to get one of the duplicates arbitrarily rather than all of them, you can speed it up by "short-circuiting" early—eg, adding a break after the print in the first one, or using isdisjoint instead of intersection in the last one. 如果您只需要知道是否有重复而不是获取它们的列表,或者只需要任意获取其中一个副本而不是所有副本,您可以通过提前“短路”来加速它 - 例如,添加在第一个中printbreak ,或者在最后一个中使用isdisjoint而不是intersection

You could try something like: 你可以尝试类似的东西:

for rec_a, rec_b in zip(List_A, List_B):
    if rec_a == rec_b:
        print "Duplicate username: {0}".format(rec_a.username)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地比较Python中的两个列表? - How to compare two lists in Python efficiently? 如何以更有效的方式比较Python中的两个文件? - How can I compare two files in Python in a more efficiently way? 我如何比较python中的两个列表,并返回第二个需要具有相同值而不管顺序? - How can I compare two lists in python, and return that the second need to have the same values regardless of order? 如何在一个循环中比较两个列表的值是否相等? - How can I compare for equality the values of two lists in one loop? 在下面提到的情况下,如何有效地比较两个列表中的字符串: - How can I efficiently compare strings in two lists in the below mentioned case: 有没有办法有效地比较python中的两个dicts列表? - Is there a way to compare two lists of dicts in python efficiently? 如果它们在同一个 object 中,我可以用一个循环实例化两个列表吗? - Can i instanciate two lists with one loop if they're in the same object? 如何比较两个列表与python中dict中的特定键 - How can I compare two lists with specific key in dict in python 如何比较python中的两个列表并通过电子邮件返回匹配项 - How can I compare two lists in python and return matches by email 如何比较python中两个列表的值? - How can I compare the values of two lists in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM