Landon G
Landon G

Reputation: 839

How to make dictionary for number of occurrences in a list

I'm trying to make a dictionary that will have the length of a string occurrence in a list as keys, and then the number of occurrences as a value.

So for example, if I had this list:

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']

the dictionary result would look something like this:

d = {3:2, 6:2, 1:2}

The key 3 represents that in the list, indexes that had a length of 3, there were 2 occurrences ('ABC' and 'BCD').

this was the best attempt I could come up with:

d = {len(x.count()):len(x.count())  for (key, value) in x} #wrong

How could I go about doing this?

Thanks for the help!

Upvotes: 1

Views: 10206

Answers (8)

ridha dabbous
ridha dabbous

Reputation: 11

def stlen(array):
    di=dict()
    for x in array:
        di.update({len(x):di.get(len(x),0)+1})
    return di

    print(stlen(['hgds','uyrd','et','hgds','uyr']))

Result:

{4: 3, 2: 1, 3: 1}

Upvotes: 1

Fallenreaper
Fallenreaper

Reputation: 10682

It seems a lot of people were answering this, but I figured to also throw my 2 cents in.

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']
def make_object(arr):
  lengths = (len(item) for item in arr);
  obj = {}
  for item in lengths:
    obj[item] = obj[item] + 1 if item in obj else 1
  return obj

make_object(x)

Upvotes: 2

Jay
Jay

Reputation: 24895

A Simple Method:

from collections import defaultdict

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']

print x

d = defaultdict(int)
for a in x:
   d[len(a)] += 1

print d

Output:

['ABC', 'GOOGLE', 'BCD', 'GOOGLY', 'A', 'A']
defaultdict(<type 'int'>, {1: 2, 3: 2, 6: 2})

Upvotes: 0

gold_cy
gold_cy

Reputation: 14216

How about this:

from itertools import groupby

d = dict()

for k, v in groupby(sorted(map(len, x))):
    d[k] = len(list(v))

{1: 2, 3: 2, 6: 2}

Upvotes: 0

dtanabe
dtanabe

Reputation: 1661

Counter is definitely the best answer in Python. A more functional-language style answer would look like this (Python 3.5 or later):

from functools import reduce

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']
length_counts = reduce(lambda accum, s: {**accum, len(s): accum.get(len(s), 0) + 1}, x, {})

or

from functools import reduce

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']
length_counts = reduce(lambda accum, i: {**accum, i: accum.get(i, 0) + 1}, map(len, x), {})

This general approach is more portable across different languages (but again in this specific question, Counter is definitely the right answer).

Upvotes: 0

xashru
xashru

Reputation: 3580

You can do it in 2 steps, first create a list of lengths of individual items and them create a dictionary from the count list.

c = [len(item) for item in x]
d = {item:c.count(item) for item in c}

Upvotes: 3

Footer
Footer

Reputation: 139

This should work

from collections import Counter
x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']
dic =Counter(list(map(len,x)))
print(dic)

Upvotes: 0

user3064538
user3064538

Reputation:

Create a Counter object of the lengths of the strings in x

from collections import Counter

x = ['ABC','GOOGLE','BCD','GOOGLY', 'A','A']

length_counts = Counter(len(word) for word in x)

print(length_counts)
# Counter({3: 2, 6: 2, 1: 2})

you can convert it to a dict if you want

print(dict(length_counts))
# {3: 2, 6: 2, 1: 2}

Upvotes: 3

Related Questions