Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all elements of 1 list are in the *same quantity* and in any order, in the list2?

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.

like image 429
Vitor Oliveira Avatar asked Mar 30 '19 19:03

Vitor Oliveira


People also ask

How do you check if all elements in list are the same?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.

How do you check if all items in a list are the same Python?

Here is a simple code with that you can check if all the elements of the list are same using the inbuilt set() method. listChar = ['z','z','z','z'] if(len(set(listChar))==1): print "All elements in list are same." else: print "All elements in list are not same."

How to check if all the elements in a list are same?

This example uses a built-in Python set datatype. Set does not allow duplicate elements. It also requires all of your elements to be hashable. Using this property of set, we can check whether all the elements in a list are the same or not. In this example, the list is converted to a set by passing the list name to set () method as shown below.

Can a list contain all the values that are the same?

There may be occasions when a list will contain all the values which are same. In this article we will see various way to verify that. We use the all function to find the result of comparison of each element of the list with the first element.

How to check if a list has only one unique element?

Set will check for each element and if all the elements in the original list are identical then the set will have just one unique element. The program will return 'Not Equal' if your list has different elements else it will return 'Equal'. This example uses count () function.

How to check if a list contains a false element?

We can check for such scenario using the below python programs. There are different approaches. In this method we grab the first element from the list and use a traditional for loop to keep comparing each element with the first element. If the value does not match for any element then we come out of the loop and the result is false.


1 Answers

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.

like image 128
gilch Avatar answered Oct 04 '22 21:10

gilch