confronting lists in a loop

G
GuidoG93 (970 points)
4 9 13
asked Dec 19, 2021 in HW8 by GuidoG93 (970 points)
recategorized Dec 21, 2021 by GuidoG93
hello, I have a list of lists of numbers, that I got with a function for combinations, for example [[1,2,3,4],[1,2,1,2],[3,4,3,4],[2,1,2,1],...]. I was trying to get combinations of those lists, with the condition that if any element in the first sublist is in the second sublist, to pass, if instead we have no elements in common, it continues, and so on until a stop point that I determined.

ex [1,2,1,2] can't have as a next element [2,1,3,4] because they have 1,2 in common, but [3,4,3,4] is instead acceptable.

Unfortunately it seems like every attempt I do doesn't return anything or it returns just a list of all combinations.

Could anyone help me?

2 Answers

Best answer
l
laertleba (2840 points)
8 26 41
answered Dec 19, 2021 by laertleba (2,840 points)
selected Dec 19, 2021 by GuidoG93
I have done a similar thing for my homework. You can use two for loops that go throught the same list of lists, then, given two lists of integers check for each of those elements if they are IN the second list. In the case one of the elements is, use Continue, otherwise do what you have to do. I hope I was helpful.
gabriel.bustamante (2540 points)
3 5 20
answered Dec 19, 2021 by gabriel.bustamante (2,540 points)
You could use sets to check whether two lists have no items in common. If a, b are two lists then set(a).isdisjoint(b) will return True if they have no items in common.

The issue could be in how you actually store the valid combinations though.