0

Should be returning a dictionary in the following format:

key_count([1, 3, 2, 1, 5, 3, 5, 1, 4]) ⇒ {
    1: 3,
    2: 1,
    3: 2,
    4: 1,
    5: 2,
}

I know the fastest way to do it is the following:

import collections

def key_count(l):
    return collections.Counter(l)

However, I would like to do it without importing collections.Counter.

So far I have:

x = []
def key_count(l):
    for i in l:
        if i not in x:
            x.append(i)
            
    count = []
    for i in l:
        if i == i:
            

I approached the problem by trying to extract the two sides (keys and values) of the dictionary into separate lists and then use zip to create the dictionary. As you can see, I was able to extract the keys of the eventual dictionary but I cannot figure out how to add the number of occurrences for each number from the original list in a new list. I wanted to create an empty list count that will eventually be a list of numbers that denote how many times each number in the original list appeared. Any tips? Would appreciate not giving away the full answer as I am trying to solve this! Thanks in advance

wjandrea
  • 28,235
  • 9
  • 60
  • 81
sak
  • 113
  • 1
  • 10
  • Don't create a list. You can do this with just a dict. The most naive solution can be done with a loop and an if-else – juanpa.arrivillaga Oct 09 '20 at 03:04
  • 1
    "I would like to do it without importing collections counter" - that sounds like insanity to me, perhaps you'd also like to do it without using loops, or any other such arbitrary limitation :-) – paxdiablo Oct 09 '20 at 03:07
  • 1
    Are you only barred from using `collections.Counter`, or are you not allowed to import anything? E.g. how about `collections.defaultdict`? – Grismar Oct 09 '20 at 03:11
  • So I am still learning the language and this problem is supposed to help me work on my knowledge on control flow and using collections.Counter won't do that – sak Oct 10 '20 at 03:21

3 Answers3

2

Separating the keys and values is a lot of effort when you could just build the dict directly. Here's the algorithm. I'll leave the implementation up to you, though it sort of implements itself.

  1. Make an empty dict
  2. Iterate through the list
  3. If the element is not in the dict, set the value to 1. Otherwise, add to the existing value.

See the implementation here:

https://stackoverflow.com/a/8041395/4518341

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Thanks for listing out the steps! I tried it as you suggested and ended up with something similar to the implementation you linked! Thanks again! – sak Oct 10 '20 at 03:27
1

Classic reduce problem. Using a loop:

a = [1, 3, 2, 1, 5, 3, 5, 1, 4]
m = {}

for n in a:
   if n in m: m[n] += 1
   else: m[n] = 1

print(m)

Or explicit reduce:

from functools import reduce
a = [1, 3, 2, 1, 5, 3, 5, 1, 4]

def f(m, n):
   if n in m: m[n] += 1
   else: m[n] = 1
   return m

m2 = reduce(f, a, {})
print(m2)
Zombo
  • 1
  • 62
  • 391
  • 407
-1

use a dictionary to pair keys and values and use your x[] to track the diferrent items founded.

import collections

def keycount(l):
    return collections.Counter(l)

key_count=[1, 3, 2, 1, 5, 3, 5, 1, 4]
x = []
dictionary ={}

def Collection_count(l):
    for i in l:
        if i not in x:
            x.append(i)
            dictionary[i]=1
        else:
            dictionary[i]=dictionary[i]+1

Collection_count(key_count)
[print(key, value) for (key, value) in sorted(dictionary.items())]
Sephe
  • 1
  • 2
  • Not to be rude, but this is really messy. `keycount` is not used, `x` is pointless cause you can also check membership in a dict (`dictionary`), [`Collection_count` should be `collection_count`](https://python.org/dev/peps/pep-0008/#function-and-variable-names), and [don't use a list comprehension for side-effects](/q/5753597) (`print`). Lastly, `dictionary` is implicitly modified by `Collection_count`, which makes debugging harder; avoid mutable global state in general; it'd be better to explicitly pass in the dict. – wjandrea Oct 09 '20 at 03:47
  • [Here's a fixed up version](https://gist.github.com/wjandrea/f3c0b58880422d13879f3703bb138836). And I forgot to mention, [avoid `l` as a variable name](https://www.python.org/dev/peps/pep-0008/#names-to-avoid); you could use `L` instead. – wjandrea Oct 09 '20 at 03:50
  • I just use the original code try to stick the initial idea, but thaks for the suggestions – Sephe Oct 09 '20 at 05:22