0

I want to make array object like this:

{'book1':3, 'book2':4, 'book3':5}

from an array like this

['book1', 'book1', 'book1', 'book2', 'book2', 'book2', 
'book2', 'book3', 'book3', 'book3', 'book3', 'book3']

How to do that? my idea is looping, but dont know to count the same value

*sorry for bad explanation

Aran-Fey
  • 30,995
  • 8
  • 80
  • 121
  • In Python that first data structure is called a [dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). Looping is certainly a valid way to accomplish what you want to do. I'd suggest reading up on dictionaries, trying it out, and updating this with your code if you have a specific question or problem. Note that it's not necessary but using [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) can provide a concise solution. – kungphu Apr 20 '19 at 04:39

4 Answers4

2

collections.Counter is a handy builtin for this exact task:

from collections import Counter

lst = ['book1', 'book1', 'book1', 'book2', 'book2', 'book2', 'book2', 'book3', 'book3', 'book3', 'book3', 'book3']
print(dict(Counter(lst)))

Output:

{'book1': 3, 'book2': 4, 'book3': 5}
ggorlen
  • 26,337
  • 5
  • 34
  • 50
0

You could do something like this:

arr = ['book1', 'book1', 'book1', 'book2', 'book2', 'book2', 'book2', 'book3', 'book3', 'book3', 'book3', 'book3']

final = {}

# loop through each item in the array
# if it is NOT found in the dictionary, put it
# in the dictionary and give it a count of 1
# if it IS found in the dictionary, increment its value
for x in arr:
    if x in final:
        final[x] += 1
    else:
        final[x] = 1

print(final)
zedfoxus
  • 28,612
  • 4
  • 47
  • 53
0

You can also use collections.Counter, which is made for exactly such thing.

from collections import Counter

li =['book1', 'book1', 'book1', 'book2', 'book2', 'book2', 'book2', 'book3', 'book3', 'book3', 'book3', 'book3']
print(dict(Counter(li)))
#{'book1': 3, 'book2': 4, 'book3': 5}
Devesh Kumar Singh
  • 19,316
  • 5
  • 17
  • 37
-2
listA=['book1', 'book1', 'book1', 'book2', 'book2', 'book2', 'book2', 'book3', 'book3', 'book3', 'book3', 'book3']
dictA={}
for x in listA:
    if x in dictA.keys():
        dictA[x]+=1
    else:
        dictA[x]=1
Noobie
  • 23
  • 1
  • 9
  • :( I'm too late @zedfoxus This is not copied – Noobie Apr 20 '19 at 04:44
  • 2
    Aside from being a code-only answer, this shadows _two_ reserved words, `list` and `dict`. There are other style issues here but that one is potentially serious; please be careful about that, and I'd recommend adopting [PEP 8](https://www.python.org/dev/peps/pep-0008/) to avoid similar issues. – kungphu Apr 20 '19 at 04:46
  • Ok I fix that already – Noobie Apr 20 '19 at 04:53
  • I think I can't get reputation and privileges anymore -_- :( – Noobie Apr 20 '19 at 04:54
  • Technically fixed. :) But I still recommend reading that PEP 8 link. Writing your code in a uniform way, the way most other Python devs do, is a good way to ensure it's easily readable, which helps with maintainability. – kungphu Apr 20 '19 at 04:56
  • @Noobie I don't mind the similarity at all. You presented a different way of examining dictionary's keys than I did. It's good for OP to see the different ways to solve the problem. – zedfoxus Apr 21 '19 at 02:30