Wednesday 15 January 2014

Ranking Elements of multiple Lists by their count in Python -


I want to rank several lists according to their elements so that they appear in each list. Example:

list1 = 1,2,3,4
list2 = 4,5,6,7
list3 = 4,1,8,9

result = 4,1,2,3,4,5,6,7,8 (4 times counted three times, 1 time twice and once again)

I have tried the following: There is something more intelligent and I can do anything with lists.

  l = [] l.append ([1, 2, 3, 4, 5]) l.append ([1, 9, 3, 4, 5]) l.append ( L1append ([1, 10, 8, 4, 5]) l.append ([1, 12, 13, 7, 5]) l.append ([1, 14, 13, 13, 6]) x1 = set (L [ 0]) and set (L [1]) and set (L [2]) and set (L [3]) x2 = set (L [0] and set (L [1]) and set (L [2] ) And set (L [4]) x3 = set (L [0]) and set (L [1]) and set (L [3]) and set (L [4]) x4 = set (L [0] ] And set (L [2]) and set (L [3]) and set (L [4]) x5 = set (L [1]) and set (L [2]) and set (L [3] ) And set (L [4]) set1 = set (x1) | Set (x2) | Set (x3) | Set (x4) | Set (x5) a1 = list (set (L [0]) and set (L [1]) and set (L [2]) and set (L [3]) and set (L [4]) a2 = Getdifference (List 1), a1) print a1 print a2  

Now there is a problem ... I can do it once again with 3, a4 and a5, but its Very complicated, I need a function for this ... but I do not know how ... I got trapped in mathematics;)

Solution: Thanks a lot for the discussion I like this system somehow: fast + Ankaripuarn you helped me! Ty

  import collection data = [[1, 2, 3, 4, 5], [1, 9, 3, 4, 5], [1, 10, 8, 4, 5], [1, 12, 13, 7, 5], [1, 14, 13, 13, 6]]] Sorted_by_count (lists): calculations for L in lists = archive .defaultdict (int): For L in L: calculation [n] + = 1 returns [num for num, sorted number (count.items (), Key = lambda k_v: (K_v [1], k_v [0]), reverse = true) printed_bankon (data)  

We now normalize it (carry any passionate move For, hushable requirement is loose), allow good Rename and reverse parameters (to sort sorted), and rename it:

  def freq_sorted (iterable, key = none, reverse = false, include_freq = False): " Returns a list of items can be sorted by return frequency. If included, item (item, freak) is returned in exchange for the replacement item. The key (item) should be heavy, but the item is not needed. * High * frequencies are returned first. In the frequency group, the item is sorted according to the key (item). "" "If there is no key: key = lambda x: x key_counts = archive .defaultdict (int) item = {} in repeated in n: k = key (n) key_counts [k] + = 1 items.setdefault ( K, n) if_freq: def get_item (k, c): return item [k], c: def get_item (k, c): return item [k] return [get_item (k, c) k, sorted in c For example:  
  

example:

  code>> gt; & gt; & gt; Import Iterooles>> print freq_sorted (itertools.chain.from_iterable (data)) [1, 5, 4, 13, 3, 2, 6, 7, 8, 9, 10, 12, 14] > Gt; print freq_sorted (itertools.chain.from_iterable (data), include_freq = true) # (slightly modified) [(1, 5), (5, 4), (4, 3), (13, 3) ), (3, 2), (2, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (12, 1) (14, 1)]  

No comments:

Post a Comment