python - 按唯一属性值过滤数据类实例

标签 python filtering python-dataclasses

我有一个数据类实例列表,其形式为:

dataclass_list = [DataEntry(company="Microsoft", users=["Jane Doe", "John Doe"]), DataEntry(company="Google", users=["Bob Whoever"]), DataEntry(company="Microsoft", users=[])]

现在我想过滤该列表并通过某个键(在本例中为公司)仅获取唯一实例。

所需的列表:

new_list = [DataEntry(company="Microsoft", users=["Jane Doe", "John Doe"]), DataEntry(company="Google", users=["Bob Whoever"])]

最初的想法是以 python 的 set()filter() 函数的方式使用函数,但这两种方法在这里都是不可能的。

到目前为止我的工作解决方案:

tup_list = [(dataclass, dataclass.company)) for dataclass in dataclass_list]
new_list = []
check_list = []
for tup in tup_list:
    if tup[1].lower() not in check_list:
        new_list.append(tup[0])
        check_list.append(tup[1].lower())

这给了我想要的输出,但我想知道是否有更Pythonic或更优雅的解决方案?

最佳答案

在您的 DataEntry 数据类中,您需要覆盖 __eq__(...)__hash__(...)函数,您可以在其中指定计算对象的哈希值时使用哪个属性以及何时将两个对象视为相等。

一个简短的示例,其中默认情况下使用类 Companyname 属性来确定两个对象的相等性。我还通过一个选项扩展了您的案例,您可以在其中确定在构造对象时将考虑唯一性的属性。请注意,要比较的所有对象都需要具有相同的comparison_attr

import pprint

class Company:

    def __init__(self, name, location, comparison_attr="name") -> None:
        # By default we use the attribute `name` for comparison
        self.name = name
        self.location = location
        self.__comparison_attr = comparison_attr

    def __hash__(self) -> int:
        return hash(self.__getattribute__(self.__comparison_attr))

    def __eq__(self, other: object) -> bool:
        return self.__getattribute__(self.__comparison_attr) == other.__getattribute__(self.__comparison_attr)

    def __repr__(self) -> str:
        return f"name={self.name}, location={self.location}"

for attribute_name in ["name", "location"]:
    companies = [
        Company("Google", "Palo Alto", comparison_attr=attribute_name), 
        Company("Google", "Berlin", comparison_attr=attribute_name),
        Company("Microsoft", "Berlin", comparison_attr=attribute_name),
        Company("Microsoft", "San Francisco", comparison_attr=attribute_name),
        Company("IBM", "Palo Alto", comparison_attr=attribute_name),
    ]

    print(f"Attribute considered for uniqueness: {attribute_name}")
    pprint.pprint(set(companies))

输出:

Attribute considered for uniqueness: name
{name=Microsoft, location=Berlin,
 name=Google, location=Palo Alto,
 name=IBM, location=Palo Alto}

Attribute considered for uniqueness: location
{name=Microsoft, location=San Francisco,
 name=Google, location=Berlin,
 name=Google, location=Palo Alto}

关于python - 按唯一属性值过滤数据类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71426366/

相关文章:

r - 过滤季节性时间序列异常值

opencv - 如何按颜色过滤OpenCv图像中的矩形区域?

python - 如何删除数组列表中数组中的就地项目

Python - 多个if语句被忽略

python - django 从查询集中返回外键对象?

python - 基于用户的过滤 :Recommendation system

python - 验证 python 数据类中的详细类型

python - 如何使数据类中的属性只读?

python - 禁止从类外部使用默认构造函数

python - tf.Estimator.train 抛出 as_list() 未在未知 TensorShape 上定义