Wilson
Wilson

Reputation: 17

searching if a key in a dictionary has a value of 2 or greater

The function histogram returns a dictionary where the keys are letters and the values are the number of times that letter appears in the argument passed to it.

def histogram(s):
    d = dict()
        for c in s:
            if c not in d:
                d[c] = 1
            else:
                d[c] += 1
    return d

I would like the function has_duplicates to use histogram. Then search the dictionary that is returned to check for any values that are greater than 1, returning either True or False.

def has_duplicates(t):
     histogram(t)

I am having difficulty accessing the values within the dictionary that's been returned by histogram.

Upvotes: 0

Views: 234

Answers (5)

Michael S.
Michael S.

Reputation: 150

You find good answers to your question in the posts, above.

But here's another approach with little code that you might think about, if your goal is just to check for any duplicate characters (or objects):

def has_dupes(t):
    return len(t) != len(set(t))

set(t) converts the string in t into a set of chars. During conversion, all duplicates are automatically removed, because sets can hold every object only once. Comparing the length of the initial string with the number of items in the created set will return True if there are no duplicates, but False if at least one duplicate has been removed during set creation. That works with other sequences as well.

Note: Negative comparison '!=' was used to make the function a positive check for dupes, instead of a negative check for no dupes.

Michael

Upvotes: 0

Endre Both
Endre Both

Reputation: 5740

If you're only interested in whether or not there are duplicates, there is no reason to loop over the entire dictionary when you've found a duplicate earlier:

def has_duplicates(t):
    for v in t.values():
        if v > 1:
            return True
    return False

collections.Counter however is useful for counting the characters in histogram().

Upvotes: 0

Amadan
Amadan

Reputation: 198436

  • You are not assigning histogram(t) to anything in your has_duplicates function.

  • You don't need to reinvent a wheel: collections.Counter will do it for you.

So... something like this?

from collections import Counter

def has_duplicates(s):
    counter = Counter(s)
    return {key: value > 1 for key, value in counter.items()}

has_duplicates([1, 2, 7, 2, 2])
# => {1: False, 2: True, 7: False}

If you meant a single True if any duplicates are detected,

def has_duplicates(s):
    counter = Counter(s)
    return any(value > 1 for value in counter.values())

has_duplicates([1, 2, 7, 2, 2])
# => True

Upvotes: 2

HariUserX
HariUserX

Reputation: 1334

Assuming its python 3

s = {"a": 1, "b":3, "c":2, "d":4, "e":1}

s_new = {k:v for k,v in s.items() if v > 1}

print(s_new)

Result:

{'d': 4, 'b': 3, 'c': 2}

For Python 2, use iteritems()

Upvotes: 0

Torben Klein
Torben Klein

Reputation: 3116

like so:

def has_duplicates(t):
   d = histogram(t)
   duplicates = [key for key, value in d.items() if value > 1]
   return True if duplicates else False
   # or just: return bool(duplicates)

Upvotes: 0

Related Questions