如何计算重复的对象属性?

How to count repeated objects attributes?

我有下一个代码,我为每个入口宠物创建对象:

from itertools import groupby
class Petshop():
    def __init__(self, entryDate, name, ownerName):
        self.entryDate = entryDate
        self.name = name
        self.ownerName = ownerName

class Pets():
    def __init__(self):
        self.petsList = []
    
    def addPets(self, entryDate, name, ownerName):
        entry_pet = Petshop(entryDate, name, ownerName)
        self.petsList.append(entry_pet)
    
    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        counter = 0
        for group in groupby(self.petsList, key=lambda p: p.entryDate):
            ls = list(group)
            print("---------------", ls[0], '------------------')
            for pet in list(ls[1]):
                print("Name:", pet.name)
                print("Owner name:", pet.ownerName)
                if pet.name in list(ls[1]):
                    counter += 1
                print('There are ',counter,'pets with the same name')

pet = Pets()

pet.addPets('04/13/2021','Pinky', 'David Smith')
pet.addPets('07/10/2020', 'Charlie', 'Joe Davis')
pet.addPets('04/13/2021','Pinky', 'Daniel Trincot')
pet.addPets('07/10/2020', 'Kenny', 'Susan Jones')
pet.addPets('12/22/2018', 'Teddy', 'Carl Johnson')
pet.addPets('07/10/2020', 'Kenny', 'Richard Campbell')
pet.addPets('04/13/2021','Max', 'Bryan Miller')
pet.addPets('07/10/2020', 'Buddy', 'Kathy Brown')
pet.addPets('07/10/2020', 'Kenny', 'John Brown')
pet.printPets()

使用该代码,我想计算按日期重复输入宠物的次数,例如,我希望在控制台中:

--------------- 04/13/2021 ------------------
Name: Pinky
Owner name: David Smith
Name: Pinky
Owner name: Daniel Trincot
Name: Max
Owner name: Bryan Miller
There are 2 pets with the same name
--------------- 07/10/2020 ------------------
Name: Charlie
Owner name: Joe Davis
Name: Kenny
Owner name: Susan Jones
Name: Kenny
Owner name: Richard Campbell
Name: Buddy
Owner name: Kathy Brown
Name: Kenny
Owner name: John Brown
There are 3 pets with the same name
--------------- 12/22/2018 ------------------
Name: Teddy
Owner name: Carl Johnson
There are 0 pets with the same name

我尝试这样做:

if pet.name in list(ls[1]):
     counter += 1

但这不起作用,因为我认为我的代码没有进入 if 而我只是进入了控制台:

--------------- 04/13/2021 ------------------
Name: Pinky
Owner name: David Smith
There are  0 pets with the same name
Name: Pinky
Owner name: Daniel Trincot
There are  0 pets with the same name
Name: Max
Owner name: Bryan Miller
There are  0 pets with the same name
--------------- 07/10/2020 ------------------
Name: Charlie
Owner name: Joe Davis
There are  0 pets with the same name
Name: Kenny
Owner name: Susan Jones
There are  0 pets with the same name
Name: Kenny
Owner name: Richard Campbell
There are  0 pets with the same name
Name: Buddy
Owner name: Kathy Brown
There are  0 pets with the same name
Name: Kenny
Owner name: John Brown
There are  0 pets with the same name
--------------- 12/22/2018 ------------------
Name: Teddy
Owner name: Carl Johnson
There are  0 pets with the same name

所以我来这里寻求帮助。

这可能不是最好的方法,但您可以这样做。应该为每个组重置计数器,正如@Matiiss 所说,ls[1] 是 PetShop class 的实例,因此将其保存在变量中可能会更好,访问其属性可以使其更加清晰。

from itertools import groupby

class Petshop():
    def __init__(self, entryDate, name, ownerName):
        self.entryDate = entryDate
        self.name = name
        self.ownerName = ownerName
        
class Pets():
    def __init__(self):
        self.petsList = []
        
    def addPets(self, entryDate, name, ownerName ):
        entry_pet = Petshop(entryDate, name, ownerName)
        self.petsList.append(entry_pet)
        
    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        
        for group in groupby(self.petsList, key=lambda p: p.entryDate):
            ls = list(group)
            data = list(ls[1])
            names= []
            print("--------", ls[0], "--------")
            index = 0
            for pet in list(data):   
                if pet.name in names :
                    repeatedName = pet.name
                index+= 1
                
                print("Name:", pet.name)
                names.append(pet.name)
                print("Owner:", pet.ownerName)
               
                if index == len(data):
                   print("There are", names.count(repeatedName), "pets with the same name")
            names = []
            
pet = Pets()

pet.addPets('04/13/2021','Pinky', 'David Smith')
pet.addPets('07/10/2020', 'Charlie', 'Joe Davis')
pet.addPets('04/13/2021','Pinky', 'Daniel Trincot')
pet.addPets('07/10/2020', 'Kenny', 'Susan Jones')
pet.addPets('12/22/2018', 'Teddy', 'Carl Johnson')
pet.addPets('07/10/2020', 'Kenny', 'Richard Campbell')
pet.addPets('04/13/2021','Max', 'Bryan Miller')
pet.addPets('07/10/2020', 'Buddy', 'Kathy Brown')
pet.addPets('07/10/2020', 'Kenny', 'John Brown')
pet.printPets()

这是我得到的输出

-------- 04/13/2021 --------
Name: Pinky
Owner: David Smith
Name: Pinky
Owner: Daniel Trincot
Name: Max
Owner: Bryan Miller
There are 2 pets with the same name
-------- 07/10/2020 --------
Name: Charlie
Owner: Joe Davis
Name: Kenny
Owner: Susan Jones
Name: Kenny
Owner: Richard Campbell
Name: Buddy
Owner: Kathy Brown
Name: Kenny
Owner: John Brown
There are 3 pets with the same name
-------- 12/22/2018 --------
Name: Teddy
Owner: Carl Johnson
There are 0 pets with the same name

printPets 方法的更改(针对 PEP 8 进行了调整,但仍然应该清楚每个名称指的是什么)(在下面的完整代码示例中有更详细的解释):

from collections import Counter

...

    def print_all_pets(self):
        for date, group in groupby(sorted(self.pets_list, key=lambda x: x.entry_date),
                                   key=lambda x: x.entry_date):
            print(f'{"-" * 20} {date} {"-" * 20}')
            counter = Counter()
            for pet in group:
                print(f"Name: {pet.name}")
                print(f"Owner name: {pet.owner_name}")
                counter[pet.name] += 1
            print(f'There are {max(counter.values())} '
                  f'pets with the same name')

完整的代码示例(它也遵循我建议你也遵循的 PEP 8);大部分解释在代码注释中:

from itertools import groupby
from dataclasses import dataclass, field
from collections import Counter


# using dataclasses because they make it easier
# to create more data driven objects and they can pre-build
# all the comparison methods so that sorting is way easier
# and you can exclude what not to sort
@dataclass(order=True)
class PetShop:
    entry_date: str
    name: field(default_factory=str, compare=False)
    owner_name: field(default_factory=str, compare=False)


class Pets:
    def __init__(self):
        self.pets_list = []

    def add_pet(self, entry_date, name, owner_name):
        entry_pet = PetShop(entry_date, name, owner_name)
        self.pets_list.append(entry_pet)

    def print_all_pets(self):
        # as you can see no key is needed for sorting the `PetShop` instances
        # because dataclass allows to do so, then just group them by their date
        # which seems to require some function, also unpack both values:
        # the date and the group
        for date, group in groupby(sorted(self.pets_list), key=lambda x: x.entry_date):
            # use f-string for formatting and also you can multiply strings
            print(f'{"-" * 20} {date} {"-" * 20}')
            # use counter for counting and update it with the pet name
            # each iteration
            counter = Counter()
            for pet in group:
                print(f"Name: {pet.name}")
                print(f"Owner name: {pet.owner_name}")
                # update the counter
                counter[pet.name] += 1
            # then get the highest count
            print(f'There are {max(counter.values())} '
                  f'pets with the same name')


pets = Pets()

# use a list to make the code less repetitive, this
# allows to loop over the list and add each item to the
# pets.pets_list less repetitively than calling the `add_pet`
# method multiple times manually
pet_shops = [
    ('04/13/2021', 'Pinky', 'David Smith'),
    ('07/10/2020', 'Charlie', 'Joe Davis'),
    ('04/13/2021', 'Pinky', 'Daniel Trincot'),
    ('07/10/2020', 'Kenny', 'Susan Jones'),
    ('12/22/2018', 'Teddy', 'Carl Johnson'),
    ('07/10/2020', 'Kenny', 'Richard Campbell'),
    ('04/13/2021', 'Max', 'Bryan Miller'),
    ('07/10/2020', 'Buddy', 'Kathy Brown'),
    ('07/10/2020', 'Kenny', 'John Brown')
]

for e_date, p_name, o_name in pet_shops:
    pets.add_pet(e_date, p_name, o_name)

pets.print_all_pets()

而您最初的问题是您检查了一个字符串是否在 PetShop 个实例的列表中,因此它总是评估为 False(以及即使正确使用的条件(实际上检查是否例如,宠物名称在宠物名称列表中),它只会给出每个群组的宠物总数)。因此,对于如上所示的计数,我建议使用 collections.Counter,因为它比手动创建算法要容易得多。

有用的资源:

在 PEP 8 上:
我强烈建议关注 PEP 8 - Style Guide for Python Code。函数和变量名称应在 snake_case 中,class 中的名称应在 CapitalCase 中。如果用作关键字参数 (func(arg='value')) 的一部分,则 = 周围不要有 space,但如果用于 =,则 space 周围有赋值 (variable = 'some value')。在运算符周围有 space(+-/ 等:value = x + y(此处除外 value += x + y))。在函数和 class 声明周围有两个空行。 Class 方法周围有一个空行。

首先使用 entryDate 分组,然后按名称分组

for i, j in groupby(pet.petsList, key=lambda p: p.entryDate):
    print(f"-------{i}-----------")
    for item1, item2  in groupby(list(j), key=lambda k: k.name):
        dic = {}
       for k1 in list(item2):
       if k1.name in dic:
            dic[k1.name] = dic[k1.name]+1
       else:
           dic[k1.name] = 1
       print(f"Name: {k1.name}")
       print(f"Owner name: {k1.ownerName}")
       print(dic)

它产生以下结果

-------04/13/2021-----------
Name: Pinky
Owner name: David Smith
Name: Pinky
Owner name: Daniel Trincot
{'Pinky': 2}
Name: Max
Owner name: Bryan Miller
{'Max': 1}
-------07/10/2020-----------
Name: Charlie
Owner name: Joe Davis
{'Charlie': 1}
Name: Kenny
Owner name: Susan Jones
Name: Kenny
Owner name: Richard Campbell
{'Kenny': 2}
Name: Buddy
Owner name: Kathy Brown
{'Buddy': 1}
Name: Kenny
Owner name: John Brown
{'Kenny': 1}
-------12/22/2018-----------
Name: Teddy
Owner name: Carl Johnson
{'Teddy': 1}

我可以在上面的代码中看到三个陷阱。

  1. groupby创建一个生成器,这将只有一次可迭代数据。
    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        counter = 0
        for group in groupby(self.petsList, key=lambda p: p.entryDate):
            ls = list(group)
            print(f'first {list(ls[1])}')
            print(f'second {list(ls[1])}')
#first [<__main__.Petshop object at 0x7fcd1001e430>, <__main__.Petshop object at 0x7fcd504428b0>, <__main__.Petshop object at 0x7fcd50446d00>]
#second []

您尝试在群组中使用 list() 是正确的,但您需要先解压缩日期和宠物。这是一个可能的修复方法

    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        counter = 0
        for date, pets in groupby(self.petsList, key=lambda p: p.entryDate):
            pet_list = list(pets)
            print(f'first {pet_list}')
            print(f'second {pet_list}')
#first [<__main__.Petshop object at 0x7fcd1001e430>, <__main__.Petshop object at 0x7fcd504428b0>, <__main__.Petshop object at 0x7fcd50446d00>]
#second [<__main__.Petshop object at 0x7fcd1001e430>, <__main__.Petshop object at 0x7fcd504428b0>, <__main__.Petshop object at 0x7fcd50446d00>]

  1. petlist 是一个宠物列表,而不是 pet_names 的列表,所以 if 语句永远不会为真,我们可以使用下面的方法解决这个问题。
    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        counter = 0
        for date, pets in groupby(self.petsList, key=lambda p: p.entryDate):
            pet_list = list(pets)
            pet_names = [p.name for p in pet_list]
            print("---------------", ls[0], '------------------')
            for pet in pet_list:
                print("Name:", pet.name)
                print("Owner name:", pet.ownerName)
                if pet.name in pet_names:
                    counter += 1
            print('There are ',counter,'pets with the same name')
  1. 计数总是等于宠物的数量。您只想检查看不见的宠物以计算重复的宠物,因为每只宠物至少在 pet_list 中出现一次。
    def printPets(self):
        self.petsList.sort(key=lambda p: p.entryDate)
        counter = 0
        for agg, vals in groupby(self.petsList, key=lambda p: p.entryDate):
            _vals = list(vals)
            name_list =[x.name for x in _vals]
            print("---------------", agg, '------------------')
            for i, pet in enumerate(_vals):
                print("Name:", pet.name)
                print("Owner name:", pet.ownerName)
                if pet.name in name_list[:i]:
                    counter += 1
            print('There are ',counter,'pets with the same name')

还有一个我在 (2) 中包含的小修复,通过取消缩进打印语句来防止打印中间计数。