ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 14 Apr 2019 19:15:27 -0500return lists that do not share all of the same elementshttp://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/ I am having Sage generate an array of lists for me, but I do not want it to give me the lists that have all of the same elements. For example, if Sage gives me
[1 2 3 4]
[2 3 4 1]
[1 3 4 2]
I only want Sage to give me [1 2 3 4]. I want to create a new array with these non-repeated lists. How do I do that? Thank you!Sun, 14 Apr 2019 11:19:37 -0500http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/Answer by Juanjo for <p>I am having Sage generate an array of lists for me, but I do not want it to give me the lists that have all of the same elements. For example, if Sage gives me</p>
<p>[1 2 3 4]</p>
<p>[2 3 4 1]</p>
<p>[1 3 4 2]</p>
<p>I only want Sage to give me [1 2 3 4]. I want to create a new array with these non-repeated lists. How do I do that? Thank you!</p>
http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46170#post-id-46170Assume that your array of lists is stored in `my_lists`. Then, the lines
sets = map(set,my_lists)
union(map(tuple,sets))
do the trick. You get a list of tuples whose elements are unique, as wanted. For example, if
my_lists = Permutations([3,1,2]).list() + Permutations([6,5,4]).list()
then the above two lines yield
[(4, 5, 6), (1, 2, 3)]Sun, 14 Apr 2019 19:15:27 -0500http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46170#post-id-46170Answer by Emmanuel Charpentier for <p>I am having Sage generate an array of lists for me, but I do not want it to give me the lists that have all of the same elements. For example, if Sage gives me</p>
<p>[1 2 3 4]</p>
<p>[2 3 4 1]</p>
<p>[1 3 4 2]</p>
<p>I only want Sage to give me [1 2 3 4]. I want to create a new array with these non-repeated lists. How do I do that? Thank you!</p>
http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46167#post-id-46167As usual, Éric is right but delphic (pythic ?)... Try this
L=[]
S=set(L)
for u in (your generator):
s=set(u)
if s not in S:
L.extend(u) ## Maybe L.extend(copy(u)) if sharing structures may be a problem later...
S.add((s)
L
At the end of execution, L should contain lists differing by at least one element.Sun, 14 Apr 2019 15:16:52 -0500http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46167#post-id-46167Answer by eric_g for <p>I am having Sage generate an array of lists for me, but I do not want it to give me the lists that have all of the same elements. For example, if Sage gives me</p>
<p>[1 2 3 4]</p>
<p>[2 3 4 1]</p>
<p>[1 3 4 2]</p>
<p>I only want Sage to give me [1 2 3 4]. I want to create a new array with these non-repeated lists. How do I do that? Thank you!</p>
http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46160#post-id-46160Use sets, not lists:
sage: set([1, 2, 3, 4])
{1, 2, 3, 4}
sage: set([2, 3, 4, 1])
{1, 2, 3, 4}
sage: set([1, 2, 3, 4]) == set([2, 3, 4, 1])
True
Sun, 14 Apr 2019 11:49:00 -0500http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?answer=46160#post-id-46160Comment by merluza for <p>Use sets, not lists:</p>
<pre><code>sage: set([1, 2, 3, 4])
{1, 2, 3, 4}
sage: set([2, 3, 4, 1])
{1, 2, 3, 4}
sage: set([1, 2, 3, 4]) == set([2, 3, 4, 1])
True
</code></pre>
http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?comment=46162#post-id-46162I am not creating the lists. I am running an iterator and Sage gives me lists. I want it to return lists with distinct elements.Sun, 14 Apr 2019 13:51:33 -0500http://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/?comment=46162#post-id-46162