Doubt about Python List

Asked

Viewed 79 times

0

I have these lists:

data_list_com_todos_os_dados = [x for x in queryset_data if x not in data_list_all]

data_list_ que conta_repetições = { y:data_list_3.count(y) for y in set(data_list_3)} 

that returns me this result:

{'2019-06-08': 2, '2019-06-06': 2, '2019-06-10': 2, '2019-06-03': 1, '2019-06-02': 2, '2019-06-04': 1}

is the result I was hoping for.

I’m using python3 and django2 to create graphs, but I’m having a hard time getting only the value of the items to put them in another or the same list keeping the positions, because then JSON can render the data and generate the graph.

I think the list I created has 3 positions: [index,valor:quantidade_de_vezes_que_se_repete] and I’m not getting just the quantidade_de_vezes_que_se_repete. I read and researched a little but I could not resolve my doubt.

1 answer

2

What you have created is not a list, it is a dictionary. A dictionary is a map that relates the key/value pair. In your case the dates are the keys and the number of times that is repeated are the values.

As you just want a list of values, just do data_list.values(). This will return a view Object, which is an eternal object. If you really need a list you can do:

data_values = list(data_list.values())

Note: recommend changing the name data_list since the structure is not a list. It makes no difference in the execution of the code, but leaves more readable.

  • I understand, thank you very much, I will give one more read on dicionario and list because I was complicated with that I swear that I was putting up lists and not dics thank you very much. and I will seedlings to dics all because readability is very important in the code, an avez que Sta getting giant the code.

  • One thing I think worth commenting on too: unless you have absolute certainty that you will not have more than one die on the same date, avoid using them as keys, as these are unique. That is, if you use dates as keys in a dictionary and have more than one data at some date, you will end up losing this data.

  • 1

    @Murilositonium Well observed, but in this case it seems not to take this risk since the stored value is precisely the amount of times that the date was repeated in the original structure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.