Sorting the dictionary

1

I have the following function that I have done that is this:

def funcao(elemento):
    x = []
    for i in grafico:
            timestring = i["CrimeTime"]
            x.append(timestring)
    x= sorted(x)

    y= Counter(x).values()
    return x,y

And what it gives me is:

(['16:40:00', '16:45:00', '17:30:00', '18:30:00', '18:38:00', '20:00:00', '21:30:00', '21:30:00', '21:51:00'], [1, 1, 2, 1, 1, 1, 1, 1])

Being what you should give me is:

Código :
(['16:40:00', '16:45:00', '17:30:00', '18:30:00', '18:30:00', '20:00:00', '21:30:00', '21:30:00', '21:51:00'], [1, 1, 1, 2 , 1,2, 1])

The goal is to get the ordered hours that give correct, and the number of times that each hour repeats, being that the values are well, they are not correctly positioned (it may have to do with the fact to get the values to the dictionary) but I'm not seeing how I get the correctly sorted values

    
asked by anonymous 17.05.2016 / 11:06

1 answer

1

Dude,

What is happening to my view is, when you call Counter(x) a dictionary is being created using the values that were in your list as keys and the number of times that value appears in the list as the value

When you call .values() in the dictionary that was created, it returns the values that are present for each key, but the dictionary does not guarantee the ordering.

If you access key by key you will see that the values are correct.

def funcao(elemento):
    x = []
    for i in grafico:
        timestring = i["CrimeTime"]
        x.append(timestring)
    x = sorted(x)        
    unique_x = sorted(set(x)) #remove os valores duplicados de x mantendo a ordenação
    count_x = Counter(x)
    y = [count_x[value] for value in unique_x]

    return x,y

I do not know what your real needs are, but I might just return the dictionary, which already contains all the information you want.

    
17.05.2016 / 12:38