0

I have the following data:

data = [('ORG', 'Apple'), ('ORG', 'Microsoft'), ('ORG', 'microsoft'), ('NAME', 'Microsoft')]
print(Counter(data))

I want to group by the tuple second item and then first item. insensitive case. So the result should be (with the count):

[(('ORG', 'apple'),1), (('ORG', 'microsoft'),2), (('NAME', 'Microsoft'),1)]

Note that 3 microsoft values became 2.

Thanks

SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • Does this answer your question? [How to count the frequency of the elements in an unordered list?](https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-an-unordered-list) – tevemadar Feb 01 '21 at 13:14

1 Answers1

0

It is not very clear how you want to group (probably need more items in the original data).

But you can benefit from the built-in Counter. Here I first lower-case the company name, then apply counter on that, and finally retrieve most_common with no args (default, all of them). Note that the order is not preserved. If that is a concern for you, then you should consider sorting afterwards.

from collections import Counter


data = [
    ('ORG', 'Apple'),
    ('ORG', 'Microsoft'),
    ('ORG', 'microsoft'),
    ('NAME', 'Microsoft'),
]


def lower_case_company(data):
    return [
        (first, second.lower())
        for first, second in data
    ]


lower_data = lower_case_company(data)
c = Counter(lower_data)
print(c.most_common())

Output

[(('ORG', 'microsoft'), 2), (('ORG', 'apple'), 1), (('NAME', 'microsoft'), 1)]
Maxim Ivanov
  • 299
  • 1
  • 6