How to count values in dictionary and print the ke

2019-07-24 11:57发布

问题:

I have created an anagram program that takes all words from a file and prints the letters followed by all of the words from the file that can be created using those letters. This is an example of what it prints out:

cinos ['cions', 'coins', 'icons', 'scion', 'sonic']

Now that I have created an anagram program that has a dictionary with the random letters as keys and the anagrams as values, I want to find the group of letters(keys) that has the largest amount of anagrams(values) and print just the values. Here is what I have written:

from collections import defaultdict

f= open('dictionary.txt')
d= defaultdict(list)
for line in f:
    strip=line.rstrip()
    key = "".join(sorted(strip))
    d[key].append(strip)
count=0
for values in d.values():
    if len(values)>count:
        count=len(values)
        print(values)

This prints all of the values ever assigned to count, but I just want to print the last entry associated in count. I tried values[-1], but that didn't work.

回答1:

It is not clear what you expect

count="".count(values)

to do, but Python is trying to interpret it as "count the number of times the string object referenced by the name values appears within the string object ''", which is coming to an inevitable and unsuccessful end as values isn't a string object and '' is empty anyway.



回答2:

d = { "taf": ["aft", "fat"], ... }
max_d = max(d.values(), key=len)

I think this is what you're trying to do. You have some dictionary d of random letters as keys, with their possible anagrams as a list of values. Your goal is to return the key with the LONGEST list of values, right?

That said, this seems INCREDIBLY memory intensive. If I were you, I'd do this instead:

lst_of_random_letters = ["abcdef", "abcdeg", "abcdeh" ... ] # however you're generating this
def make_anagrams(letters):
    # however you're implementing this
    # but return a list of the anagrams, e.g.
    # [IN] : cions
    # [OUT]: ['cions', 'coins', 'icons', 'scion', 'sonic']

longest_anagrams = max((make_anagrams(letters) for letters in lst_of_random_letters), key=len)


回答3:

your error is caused by:

count="".count(values)

to which you give values which has been defined as a list in:

d = defaultdict(list)

as the help page of count states:

count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

it's unlikely to work.

instead prefer:

largest_group = max(d, key=lambda x: len(d[x]))

as suggested by @AdamSmith (disclaimer: I was about to write the same thing as you posted your answer)



回答4:

This calls len on each value and returns the longest value

max(d.values(), key=len)

If 2 or more values have the same length, you'll still just get one of them