Collections module in Python behaves strange? -


Hi all Python user,

I'm not 100% sure, but it seems that a bug In Python Function collections.Counter () is a part of the archive module. Function collections.Counter () should count the incidents of numbers in a list; in other words, the frequency of the number of vector in this list should be determined. It does not matter whether these numbers are floats or integers in many occasions but this does not work properly, and this is an example when it fails to answer correctly:

  Import as NPP as import Import Import from import Import imported by import Import plots as matplotlib.pyplot = [250, 250, 251, 251, 251, 251, 251, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254] data_2 = [250, 250, 251, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254 , 254, 254, 254, 254, 255, 256, 256] #s Not allowed for dataDei D3 = D. 2.values ​​() Approved wavelengths d2 = collections.Counter (data_1) in the archive module # for iterations for bears 
on
  [2, 4, 4, 4, 5]   using pre> 

All entries will receive the correct answer (D3). However, if I apply data_2 to archive , then I get the following answer (D 3):

  [2, 2, 4, 4, 4, 5, 1] ​​ 

What is wrong [2 , 4, 4, 4, 5, 1, 2] should be read. It looks like archive. Control () exchange some elements, but I do not get any logical explanation, why does it. Btw I am using Python 2.7.5

What is your opinion on this?

P.S. 1: Dictionary for the function dict (in the data for i, data_2 count (i)) i) 2 data_2 for Gives the same wrong answer for the collection. Control (Data_2)

PS2: Resolution It looks like by group > itertools module : Importing itertools for group

  group by group group (data2)] The group has a subtype of d3 = [lane (list (group)) = "text"> 

dict and does not have a sequence of dictionaries such as, you can call on the counter object (dictionary!) Outputs using d.values ​​() are arbitrary and it is unlikely that the incoming What do you want?

If you want to order values ​​you receive from the key, you should sort the most important values ​​first and then return the value:

  & gt; & Gt; & Gt; Sorted for [V (counter (data_1) .im (), key = lambda x: x [0])] [2, 4, 4, 4, 5]> gt; & Gt; & Gt; [V for Kashmir, in order (counter (data_2) .im (), key = lambda x: x [0])] [2, 4, 4, 4, 5, 1, 2]  < / Ex> 

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

c# - WPF Expander overlay used in ItemsControl -

android - how to get distance of 2 beacons and make a condtion base on that distance -