Ask Your Question
0

remove list element from a list of list with symbolic pi

asked 2023-03-04 09:37:02 +0100

ortollj gravatar image

HI

I'm reporting something that seems odd to me.

I want to remove from a list of lists an element which is itself a list. if I convert the elements to string before comparing them the two lists are equivalent, but if I don't do the string conversion, then SageMath finds a difference between the two lists. equiv which was written by hand is ok, we can remove an element from this list, while the one made by Arrangement does not allow this element to be removed without first having it converted to a string.

upAndRightSidesL=[[2*pi, 2*pi], [0, 2*pi], [pi, 2*pi], [3/2*pi, 2*pi], [1/2*pi, 2*pi], [2*pi, pi], 
                  [2*pi, 0], [2*pi, 3/2*pi], [2*pi, 1/2*pi]]
equiv=[[2*pi, 2*pi], [2*pi, 0], [0, 2*pi], [0, 0]]
print('equiv not build by Arrangements: ', equiv)

## if these 3 lines are not commented then [2*pi, 2*pi] is not removed in newL
#for pt in upAndRightSidesL :
#    if pt[0]==2*pi and pt[1] ==2*pi:
#        equiv=Arrangements( [pt[0]] + [0] + [pt[0]] + [0]   ,2).list()

print('upAndRightSidesL : ', upAndRightSidesL)
print('equiv build by Arrangement : ', equiv)

print(upAndRightSidesL)
def removeFromList(el,L) :
    newL=[]
    for index in range(len(L)) :        
        #if str(el) != str(L[index]) : # if string conversion beforehand then both list are ok
        if el != L[index] :
            newL.append(L[index])
        else :
            print ('not included',str(el))
    return newL    




newL=removeFromList(equiv[0],upAndRightSidesL) 

show('newL : ',newL)
edit retag flag offensive close merge delete

Comments

oops I forgot to write this precision for versions !!.

W11 WSL UBUNTU 22.04 SageMath 9.8

ortollj gravatar imageortollj ( 2023-03-04 11:54:13 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-03-04 16:52:17 +0100

Max Alekseyev gravatar image

updated 2023-03-04 16:54:03 +0100

The reason for the observed behavior is that the elements of Arrangements are not lists but

<class 'sage.combinat.permutation.Arrangements_msetk_with_category.element_class'>

and so they cannot be equal to lists even if the content is the same.

A simple fix is to perform conversion into a list in the removal call:

newL=removeFromList(list(equiv[0]),upAndRightSidesL)

Btw, your removeFromList function is an overkill - it can be done in a single line using list comprehension:

def removeFromList(el,L) :
    return [e for e in L if e!=el]
edit flag offensive delete link more

Comments

Thank you @Max Alekseyev.

I had not suspected for a second that the function :

Arrangements( [pt[0]] + [0] + [pt[0]] + [0]   ,2).list()

does not return a true list !!

ortollj gravatar imageortollj ( 2023-03-04 17:22:06 +0100 )edit

It returns a list of Arragements elements.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-03-04 17:34:26 +0100 )edit

Oops ok , sorry.

ortollj gravatar imageortollj ( 2023-03-04 17:57:49 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-03-04 09:37:02 +0100

Seen: 239 times

Last updated: Mar 04 '23