How to create a condition "if all the elements of a list then inside a set list" (Python)?

Asked

Viewed 92 times

1

I’m trying to say, "If all elements of a list are in a list of sets".

a= [0,1,2,3,4]
b=[ 3,4,5,6,7]
c=[0,1,8,9,10]
d=[0,1]

e= set(a).intersection(b)
f = set(a).intersection(c)

g = [e,f]
print (g)

if all(d) in g : # aqui está meu problema 
    print ('done')

Since the list d is equal to [0,1] and the set list (sets) g is equal to [{3, 4}, {0, 1}], why 'done' is not printed?

1 answer

3


His question was: As the list d is equal to [0.1] and the list of sets (sets) "g" is equal to [{3, 4}, {0, 1}], because 'done' is not printed?

The answer to the question is: "Since d is a list and g is a list of sets, Python compares whether they are the same object type before comparing the elements. It is also important to note that equality between lists (list_a == list_b) is only true if both have the same elements and in the same order."

'''python

a= [0,1,2,3,4] # Isso é uma lista, lista são conjuntos ordenados de dados
b=[ 3,4,5,6,7] # isso também é uma lista
c=[0,1,8,9,10] # também
d=[0,1]        # idem

e= set(a).intersection(b) # Aqui você criou um set a partir da lista a e tirou a interceção com b, o resultado é um set
f = set(a).intersection(c) # Sets são conjuntos não ordenados de dados

g = [e,f] # aqui você criou G como uma lista de sets [{3,4},{0,1}]
print (g)


#if all(d) in g:      
#    print ('done')
print(all(d)) # resulta em False
print(False in g,all(d) in g) #resulta em False tambem.
print(d in g) # Também é False, d é uma lista com 2 inteiros, g é uma lista com 2 sets
print(set(d) in g) # Aqui você converte d de lista para set antes de fazer a procura, então ele retorna True

if set(d) in g:
    print('done')

''' - It’s not very good, and in general it doesn’t work, to make comparisons between two different types unless there is some form within the object that compares it...

  • If g was a set and not a set list you could use issubset() or issuperset() (reference https://www.w3schools.com/python/python_sets.asp)

  • Another alternative is, you want the list d = [0.1] to be considered within something like h = [{0.1,2},{3.4,5}] because d is contained in set 1 of the list h

Soon:

d_set = set(d) # convertendo o tipo

contido = d_set.issubset(h) # cria uma lista com a resposta se d está contido em algum conjunto de d

if any(contido): # ou, juntando as linhas: if any(set(d).issubset(h)):
   print('done')

The channel Ignorance Zero (in Portuguese) on youtube explains this very well in the following classes: Lesson 32: List comparison Class 117: Sets

  • How are you, Pedro? Considering that the site is in Portuguese, translate the English parts of your answer. You can also suggest an edition in the question by translating it into Portuguese. Looking at the AP profile it is clear that he is Brazilian and, as usually happens, confused the site that asked the question with Soen. ;D

  • Done, Lipespry I said I would reply in Portuguese and I contradicted myself at the end kkkkkkk

  • You can take the English part of your answer. His question HAS to be translated. It will be a "hole" in your answer. kk

  • Okay, I suggested editing his question but she’s a little ambiguous. I gave answers to all the interpretations I could think of.

  • Now it’s filet! ;)

  • 1

    Thanks! : D I took the opportunity to add some references of study of sets and lists.

  • Then give a read on Formatting that will leverage your answers and questions. ;)

  • 1

    I know Markdown, HTML and Latex my real problem is writing in a nice way using what I know of Markdown and HTML kkkkkkkkkk But thanks for the tip, I’ll take a look because Markdown varies from platform to platform (Git, Jupyter, etc)

  • Thank you, it worked. And vlw by references tbm !

  • You’re welcome @Vitoroliveira ! If you can accept the answer I appreciate (I’m trying to build a reputation for at least being able to comment livrement no stackoverflow :3 )

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.