12

I know its a very common question at first, but I haven't found one that specific. (If you do, please tell me.) And all ways I found didnt work for me. I need to check if all elements of list 1 appears in the same amount in the list2.

Ex :

#If list1 = [2,2,2,6]    
# and list2 =[2,6,2,5,2,4]     
#then all list1 are in list2.
#If list2 = [2,6] then all list1 are not in list2.

i'm trying this way :

list1 = [6,2]

import itertools

for i in itertools.product((2,4,5,1), repeat=3) :
    asd = i[0] + i[1]
    asd2= i[1] + i[2]

    list2 = [asd, asd2]
    if all(elem in list2  for elem in list1):
        print (i,list2)

It works when the elements are not repeated in the list1, like [1,2]. But when they are repeated, all repeated elements is beeing counted as only 1 : [2,2,2] its beeing understanded as [2]. Or so i think.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Vitor Oliveira
  • 327
  • 2
  • 10
  • 1
    After reading both questions, it does **not** look like a duplicate to me. This question cares about quantity, but not order. The other one cares about order, but not quantity. – gilch Mar 30 '19 at 20:38
  • Possible duplicate of [Checking if list is a sublist](https://stackoverflow.com/questions/35964155/checking-if-list-is-a-sublist) – Noctis Skytower Mar 31 '19 at 04:04
  • Would it be correct to title this "Unordered comparison between lists"? – Ben Voigt Mar 31 '19 at 06:12

3 Answers3

18

Use collections.Counter to convert to a dict_items view Set of (value, count) pairs. Then you can use normal set operations.

from collections import Counter

def a_all_in_b(a, b):
    """True only if all elements of `a` are in `b` in the *same quantity* (in any order)."""
    return Counter(a).items() <= Counter(b).items()

Note that Counter works on hashable elements only, because it's a subclass of dict.

gilch
  • 10,813
  • 1
  • 23
  • 28
4

Modify this answer to Checking if list is a sublist to check for equality of occurences:

from collections import Counter 

list1 = [2,2,2,6]    
list2 =[2,6,2,5,2,4]

def same_amount(a,b):
    c1 = Counter(a)
    c2 = Counter(b)

    for key,value in c1.items():
        if c2[key] != value:
            return False
    return True


print(same_amount(list1,list2))
print(same_amount(list1 + [2],list2))

Output:

True
False

There is almost no transfere-knowledge needed to create this answer, thats why I suggested it as dupe. This question is simply a more specific case of what Checking if list is a sublist discussed.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1
sub_list = [1, 1, 2, 3, 3, 3, 4, 4] # list1
test_list =  [int(i) for i in input()] # list2




def convert(_list): # Converting list to dict with frequency 
    _dict ={}
    for number in _list:
        if number in _dict:
            _dict[number] += 1
        else:
            _dict[number] = 1
    return _dict

sub_dict = convert(sub_list)
test_dict = convert(test_list)

value = { k : sub_dict[k] for k in set(sub_dict) - set(test_dict) } # comparing frequency 

print({True : "False", False : "True"}[len(value) >0 ])

produces

1234
True

[Program finished] 
21
False

[Program finished]

Counter module is best fit tho

Subham
  • 397
  • 1
  • 6
  • 14