Going through 2 lists with array data

This causes me a headache, and it's hard for me to find a solution with a for loop.

Basically, my data looks like this:

short_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] long_list = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13] ] 

I need to know how many times each number from each row in short_list appears in each row of long_list, and a comparison is NOT needed when both indexes on the list are the same because they come from the same dataset.

Example: I need to know the amount of each number in [1, 2, 3] on the long_list [2, 3, 4, 5, 6], [6, 7, 8, 9, 10] and [9, 10, 11, 12, 13]. Then go to the next row of data in short_list, etc.

+5
source share
5 answers
 for L1 in short_list: for L2 in long_list: if not set(L1).issubset(set(L2)): for x in L1: print("{} has {} occurrences in {}".format(x, L2.count(x), L2)) 
+1
source

Here is one way to do it. This is straight from my head, so there is probably a much better way to do this.

 from collections import defaultdict short_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] long_list = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13] ] occurrences = defaultdict(int) for i, sl in enumerate(short_list): for j, ll in enumerate(long_list): if i != j: for n in sl: occurrences[n] += ll.count(n) >>> occurrences defaultdict(<class 'int'>, {1: 0, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 0, 8: 0, 9: 1, 10: 1, 11: 0, 12: 0}) 

Note that enumerate() used to provide indexes during iteration. Indexes are compared to ensure that the sheets in the same relative position are not compared.

The result is a dictionary typed by items from a short list with values ​​that are the total number of this item in a long list, without subscriptions with the same index.

+3
source

This is a brute force decision. I made adjustments to the source data to make the results more interesting:

 from collections import Counter from toolz import concat short_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] long_list = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [2, 3, 11, 12, 13] ] for idx, i in enumerate(short_list): long_list_filtered = (x for x in concat(long_list[:idx] + long_list[idx+1:]) if x in set(i))) print(idx, Counter(long_list_filtered)) # 0 Counter({2: 2, 3: 2}) # 1 Counter({4: 1, 5: 1, 6: 1}) # 2 Counter() # 3 Counter({10: 1}) 
+2
source

Possible approach :

  • Navigate through each list in short_list .
  • Smooth the list into long_list , which is not the same index as the current list, and convert it to a set.
  • Create collections.Counter() to save the counts for each item in the short list that appears in the flattened list.

Demo:

 from collections import Counter from itertools import chain short_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] long_list = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13]] for i, short_lst in enumerate(short_list): to_check = set(chain.from_iterable(long_list[:i] + long_list[i+1:])) print(Counter(x for x in short_lst if x in to_check)) 

Output:

 Counter({2: 1, 3: 1}) Counter({4: 1, 5: 1, 6: 1}) Counter({9: 1}) Counter({10: 1}) 
+2
source
 short_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] long_list = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13] ] occ = [] for si in short_list: occi = [] for i, e in enumerate(si): count = 0 for li in long_list: for j, e1 in enumerate(li): if i == j: continue elif e == e1: count += 1 occi.append(count) occ.append(occi) print occ 

This should work, Happy coding :)

0
source

Source: https://habr.com/ru/post/1275194/


All Articles