[Tutor] __getitem__

monikajg at netzero.net monikajg at netzero.net
Wed Nov 23 07:25:21 EST 2016


Hi:
I have two questions in regards to below code:
1. largest is a list, not a list of lists. 
[('deit', 4), ('acer', 3), ('aceilmr', 2), ('arst', 2)]
so why when I do largest[0] I get the whole list again, not just the first item from the list. To get the first item I have to do largest[0][0].

2. largest = [sorted(analist, key=lambda analist: analist[1], reverse=True)]
brings back the same result as:
largest = [sorted(analist, key=lambda d: d[1], reverse=True)]
and the same result as:
largest = [sorted(analist, key=lambda x: x[1], reverse=True)]
The result is:
[('deit', 4), ('acer', 3), ('aceilmr', 2), ('arst', 2)]

I really do not understand why and how this works. Could you please explain?
in lambda x: x[1] I pass x to lambda and it does calculation of x[1] but where does it get the x, what is the value of x? 
why lambda x: x[1] brings the same result as lambda d: d[1] and lambda analist: analist[1]

#question. have a list of words. check for anagrams
#count how many anagrams there are.
#do it thru dictionary
#then get anagaram list which has the biggest amount of words
words = ["miracle", "claimer", "care", "race", "arts", "rats", "acre","diet", "edit", "tide", "tied"]
def anagram(words):
    d = {}
    for word in words:
        wordl = list(word)
        print "word as list:  " , wordl
        wordlsorted = sorted(wordl)
        print "word lsit sorted:  " , wordlsorted
        wordsorted = ''.join(wordlsorted)
        print "word sorted as string:   ", wordsorted
        d[wordsorted] = d.get(wordsorted, []) + [word]
        
    print d
    analist = [(key , len(value)) for key, value in d.items()]
    print analist
    print "largest:  ", [sorted(analist, key=lambda analist: analist[1], reverse=True)][0]
    largest = [sorted(analist, key=lambda analist: analist[1], reverse=True)]
    print type(largest)
    print largest[0][0]
    
anagram(words)

Thank you very much in advance for explaining this.
Monika
____________________________________________________________
Eat This Junk Food To "Reverse" Dementia
Nutrition and Healing
http://thirdpartyoffers.netzero.net/TGL3241/58358b1017902b0f1d93st02duc


More information about the Tutor mailing list