-2

Is there any way to calculate the counting of names in the list?

The following example is written by using the method of count().

Then, the outcome should be the dictionary as the same output.

Attention: do not use built-in count() method and should return a dictionary. If the list is empty, it will return an empty dictionary.

a=['Mary', 'David','Grace','Curtis','Alice','Joe','Grace']

# use count
print(dict(map(lambda x  : (x , list(a).count(x)) , a)))

output: {'Mary': 1, 'David': 1, 'Grace': 2, 'Curtis': 1, 'Alice': 1, 'Joe': 1}

AAlex
  • 63
  • 8
  • See here: https://stackoverflow.com/a/5829377/365102 – Mateen Ulhaq Jun 03 '18 at 04:10
  • @Mateen Ulhaq the way you introduce is also using count method. The answer should not use pandas or count. – AAlex Jun 03 '18 at 04:15
  • Oh. It's pretty simple to roll your own, no? Just loop over all items. Increment counts depending on the item. https://repl.it/@SicariusNoctis/BreakableAromaticClosedsource – Mateen Ulhaq Jun 03 '18 at 04:50
  • @Mateen Ulhaq you used built-in count method. – AAlex Jun 03 '18 at 05:05
  • What is a built in count method and how did I use it? – Mateen Ulhaq Jun 03 '18 at 06:06
  • @MateenUlhaq ok, you did not use count method. but your answer is defaultdict(, {'Mary': 1, 'David': 1, 'Grace': 2, 'Curtis': 1, 'Alice': 1, 'Joe': 1}). return so many unneccessary words. – AAlex Jun 03 '18 at 06:32
  • `print(dict(counts))`. Alternatively, you can get by without using `defaultdict` at all, but it's the recommended style. – Mateen Ulhaq Jun 03 '18 at 06:36

1 Answers1

0
from collections import Counter
c = dict(Counter(long_list_with_duplicates))
print(c)
ThePoetCoder
  • 182
  • 8