mardi 18 juillet 2017

Create a list from a list using a statement that call the second element of each index

This is my initial list:

    >>> List1 = (List.most_common())
    >>> print (List1)
    >>> [('d', 17), ('a', 17), ('c', 17), ('q', 6), ('w', 4), ('s', 3), ('i', 2), ('p', 2), ('f', 2), ('h', 2), ('n', 2), ('g', 2), ('j', 2), ('u', 1), ('b', 1)]

Generated from a text, it is the number of times each letter appears in the text.

    >>> HighestFactor = List1[0][1]

HighestFactor = 17 is what i get, as the highest number that letter(s) appears is always gonna be on that position

What I need to do is get the one that appears most, on this case, ( "D", "A" and "C" ) if there are more then 1 with the same number of appearance sort them alphabetically, if not just print the letter. I first though on creating a list that only have the ones equals to the HighestFactor, so i could sort alphabetically that list and get what i need

    >>> Last_list = for (x,y) in List1
                     if y = HighestFactor:

This was the closest i could get from the solution, i think, but it didn't worked.

Think on it as :

('d', 17) = (x,y)

('a', 17) = (x,y)

('c', 17) = (x,y)

and the

17 = y = HighestFactor

Do a list with only the elements that have their y = HighestFactor.

Result should be :

    >>> print Last_list
    >>> [('d', 17), ('a', 17), ('c', 17)]

Aucun commentaire:

Enregistrer un commentaire