ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 08 Mar 2021 18:50:27 +0100Multiple intersection between listshttps://ask.sagemath.org/question/56058/multiple-intersection-between-lists/This is a toy example. Suppose I have a lot of lists (whose the number is not fixed as in the example and I want to determine the intersection of the list like
L=[["B", "C", "D", "E"],["A", "C", "D"],["A", "B","D", "E"], ["A", "B","D", "C"] ]
show(L)
interL1=[x for x in L[0] if x in L[1]]
show(interL1)
interL2=[x for x in L[0] if (x in L[1] and x in L[2])]
show(interL2)
interL3=[x for x in L[0] if (x in L[1] and x in L[2] and x in L[3])]
show(interL2)
But, as the number of list (here 4), I would like to know if there is a possibility to define an operator `and` like `and(L[i],i,0, 3)`Mon, 08 Mar 2021 18:17:54 +0100https://ask.sagemath.org/question/56058/multiple-intersection-between-lists/Answer by Max Alekseyev for <p>This is a toy example. Suppose I have a lot of lists (whose the number is not fixed as in the example and I want to determine the intersection of the list like</p>
<pre><code>L=[["B", "C", "D", "E"],["A", "C", "D"],["A", "B","D", "E"], ["A", "B","D", "C"] ]
show(L)
interL1=[x for x in L[0] if x in L[1]]
show(interL1)
interL2=[x for x in L[0] if (x in L[1] and x in L[2])]
show(interL2)
interL3=[x for x in L[0] if (x in L[1] and x in L[2] and x in L[3])]
show(interL2)
</code></pre>
<p>But, as the number of list (here 4), I would like to know if there is a possibility to define an operator <code>and</code> like <code>and(L[i],i,0, 3)</code></p>
https://ask.sagemath.org/question/56058/multiple-intersection-between-lists/?answer=56059#post-id-56059Since you talk about intersection, it is easier to convert lists to sets and use `set.intersection` function, like this:
L=[["B", "C", "D", "E"],["A", "C", "D"],["A", "B","D", "E"], ["A", "B","D", "C"] ]
set.intersection(*(set(Li) for Li in L))Mon, 08 Mar 2021 18:38:56 +0100https://ask.sagemath.org/question/56058/multiple-intersection-between-lists/?answer=56059#post-id-56059Answer by tmonteil for <p>This is a toy example. Suppose I have a lot of lists (whose the number is not fixed as in the example and I want to determine the intersection of the list like</p>
<pre><code>L=[["B", "C", "D", "E"],["A", "C", "D"],["A", "B","D", "E"], ["A", "B","D", "C"] ]
show(L)
interL1=[x for x in L[0] if x in L[1]]
show(interL1)
interL2=[x for x in L[0] if (x in L[1] and x in L[2])]
show(interL2)
interL3=[x for x in L[0] if (x in L[1] and x in L[2] and x in L[3])]
show(interL2)
</code></pre>
<p>But, as the number of list (here 4), I would like to know if there is a possibility to define an operator <code>and</code> like <code>and(L[i],i,0, 3)</code></p>
https://ask.sagemath.org/question/56058/multiple-intersection-between-lists/?answer=56060#post-id-56060A better data structure than lists to make intersectoins is set, so you can first transform your list of lists into a list of sets:
sage: Lset = [set(l) for l in L]
sage: Lset
[{'B', 'C', 'D', 'E'},
{'A', 'C', 'D'},
{'A', 'B', 'D', 'E'},
{'A', 'B', 'C', 'D'}]
Then, you can pop the first set of your list to start with:
sage: s = Lset.pop()
sage: s
{'A', 'B', 'C', 'D'}
sage: Lset
[{'B', 'C', 'D', 'E'}, {'A', 'C', 'D'}, {'A', 'B', 'D', 'E'}]
Now you can intersect `s` with the remaining sets in `Lset`:
sage: s.intersection(*Lset)
{'D'}
Mon, 08 Mar 2021 18:50:27 +0100https://ask.sagemath.org/question/56058/multiple-intersection-between-lists/?answer=56060#post-id-56060