0

I am currently trying to find the count of pair occurrences in a dictionary where each key has more than one value.

example, here is a dict:

df_dict = {
'apple': ['1.0', '3.0'],
'banana': ['2.0', '5.0'],
'orange': ['1.0', '3.0'],
'plum': ['2.0', '1.0']
}

result that I want: ['1.0', '3.0']: 2 ['2.0', '5.0']: 1 ['2.0', '1.0']: 1

where apple and orange have the same pair value.

Here is what I tried but result does not match with what I want.

def pair_count(df_dict):
    count = 0
    for k, v in  df_dict.items():
        if len(set(v))' != 1:
           for idx in v:
              if idx == '1.0':
                 count = count + 1
           print(v, count)

Thank you for your time and help.

icedtea
  • 5
  • 2
  • 1
    Lists can't be keys of dicts. If you convert the lists to tuples you can use `collections.Counter` on the dict values to easily retrieve a similar result. – Michael Butscher Aug 16 '23 at 20:46

2 Answers2

1

You can use Counter:

from collections import Counter

print(Counter(map(tuple, df_dict.values())))

This outputs:

Counter({('1.0', '3.0'): 2, ('2.0', '5.0'): 1, ('2.0', '1.0'): 1})

If you want the exact output you were trying to print, then:

for key, count in Counter(map(tuple, df_dict.values())).items():
    print(list(key), count)

Output:

['1.0', '3.0'] 2
['2.0', '5.0'] 1
['2.0', '1.0'] 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

This approach is most syntactically similar to what you already tried. It uses a dictionary to store each unique value, and keeps track of how many times that unique value is seen.

def pair_count(df_dict):
  counts = {}
  for k, v in  df_dict.items():
    if len(set(v)) > 1: #not != 1 in case len is 0
       if tuple(v) in counts:
          counts[tuple(v)] += 1
       else:
          counts[tuple(v)] = 1
  print(counts)

which outputs

{('1.0', '3.0'): 2, ('2.0', '5.0'): 1, ('2.0', '1.0'): 1}
teddyzhng
  • 80
  • 7