0

I am writing an anagram program. The user inputs the length of the word they want for example 11 then my program stores the sorted words in a dict key while the values are the anagrams stored in like in my sample ouput. I only print the values with the largest set with most words for example in sample output its 3 words. But if there is another value with 3 words i would like that to be printed also.

Sample Output: Prints this only:

['activations', 'cavitations', 'vacationist']

but i also want:

['paternoster', 'penetrators', 'transportee']

Code

def main():
    wordList = readMatrix()
    sortDict = (lengthWord(wordList))
    maxSet = max(sortDict.values(), key = len)
    print(sorted(maxSet))
    for wor in sortDict.values():
        if len(maxSet) == len(sortDict.values()):
            print(sortDict.values())

def readMatrix():
    wordList = []
    strFile = open("wordsEn.txt", "r")
    lines = strFile.readlines()
    for line in lines:
        word = line.rstrip().lower()
        wordList.append(word)
    return tuple(wordList)

def lengthWord(wordList):
    lenWord = 11
    sortDict = {}
    wordList = readMatrix()
    for word in wordList:
        if len(word) == lenWord:
            sortWord = ''.join(sorted(word))
            if sortWord not in sortDict:
                sortDict[sortWord] = set()
            sortDict[sortWord].add(word)
    return sortDict



main()
wenzul
  • 3,948
  • 2
  • 21
  • 33
solo fisher
  • 65
  • 2
  • 10
  • What do you mean when there is two? you mean {2,2,5,4,6,7,99,3,5,1,99,3} and you want to print out both 99? – Steven Oct 29 '14 at 01:12
  • Sorry, this makes no sense. Now you have sample output. Without describing your problem... Just a "but I also want"... Try to read your question once whereas you don't know anything about your own problem. – wenzul Oct 29 '14 at 01:27
  • updated once again i dont know how i can be more descriptive haha – solo fisher Oct 29 '14 at 01:32
  • FWIW, your code would be easier for others to understand if you started following [PEP 8 -- Style Guide for Python Code](http://legacy.python.org/dev/peps/pep-0008/) -- especially with respect to the naming of variables and functions. – martineau Oct 29 '14 at 02:16

3 Answers3

1

What about this?

d1 = dict()
d1[0] = 5
d1[1] = 9
d1[2] = 9
d1[3] = 4

maxValue = max(d1.values()) # figure out the max value
print( [key for key in d1 if d1[key]==maxValue] ) # for keys
print( [d1[key] for key in d1 if d1[key]==maxValue] ) # for values

output using Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32:

>>> d1 = dict()
>>> d1[0] = 5
>>> d1[1] = 9
>>> d1[2] = 9
>>> d1[3] = 4 
>>> maxValue = max(d1.values()) # figure out the max value
>>> print( [key for key in d1 if d1[key]==maxValue] ) # for keys
[1, 2]
>>> print( [d1[key] for key in d1 if d1[key]==maxValue] ) # for values
[9, 9]
gmas80
  • 1,218
  • 1
  • 14
  • 44
  • Each value in the OP's dictionary is a `set` of words, not a single value like those in the `dict` shown in your answer. Other than that, I think this is basically the right (or at least the fastest) approach (and it's very similar to the accepted answer in the duplicate question). – martineau Oct 29 '14 at 02:22
  • @martineau: This is the output from the interpreter.. I modified my answer to clearly show it. – gmas80 Oct 29 '14 at 02:32
  • 1
    All I was saying is that the values your dictionary are each single values and the ones in the OP's are `sets` -- so, as is, your code wouldn't work for them (like it did for you), but it's the basically the right idea. – martineau Oct 29 '14 at 02:38
  • Ops! I didn't notice that.. – gmas80 Oct 29 '14 at 02:41
0

max returns the maximum value, not the item. You could figure out the max value by doing max(d.values()) and do a linear search on each key to see whether it is equal to the maximum value.

Kaung Htet
  • 577
  • 1
  • 11
  • 28
0

If I understood you right I would do something like:

m = max(sortDict.values())
print [v for v in sortDict.values() if len(v) == m]

Or the more explicit way

m = max(sortDict.values())
for val in sortDict.values():
    if len(val) == m:
        print val
wenzul
  • 3,948
  • 2
  • 21
  • 33
  • i get this error AttributeError: 'dict' object has no attribute 'itervalues' – solo fisher Oct 29 '14 at 01:49
  • @solofisher Python version? – wenzul Oct 29 '14 at 01:49
  • If you're using Python 3, just use `sortDict.values()`. – martineau Oct 29 '14 at 01:50
  • @martineau sure, but getting an iterator is better I think. But we can just replace it if we don't care about. And I think it's not exactly an duplicate. He wants to compare the length not the value. But basically it's the same if he can transfer the duplicate to his needs. – wenzul Oct 29 '14 at 01:51
  • In Python 3 `values()` is an iterator by default. BTW, your answer is just an adaptation of the [accepted answer](http://stackoverflow.com/a/3989032/355230) to this duplicate [question](http://stackoverflow.com/questions/3989016/how-to-find-positions-of-the-list-maximum). – martineau Oct 29 '14 at 01:53
  • changed sortDict.values() but then the ouput is just: []. nothing else and also i cant use enumarate, which is why that answer didnt help me – solo fisher Oct 29 '14 at 01:53
  • @solo fisher: Actually you need `max(map(len, sortDict.itervalues()))` if each value is a sequence -- what you call a set -- of words. – martineau Oct 29 '14 at 01:56
  • @martineau Thanks for the hint. Didn't know that. I thought it more than once yet I should use more Python 3... :) – wenzul Oct 29 '14 at 01:56
  • if i do itervalues i still get AttributeError: 'dict' object has no attribute 'itervalues' python 3 – solo fisher Oct 29 '14 at 02:00
  • @solofisher It's your problem so please just read carefully or lookup in the documentation. _In Python 3 `values()` is an iterator by default._ So just use `values`. – wenzul Oct 29 '14 at 02:03
  • Yea sorry about that shouldve tried values, thanks for your help – solo fisher Oct 29 '14 at 02:13