First time here? Check out the FAQ!

Ask Your Question
0

remove list element from a list of list with symbolic pi

asked 2 years ago

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)
Preview: (hide)

Comments

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

W11 WSL UBUNTU 22.04 SageMath 9.8

ortollj gravatar imageortollj ( 2 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 2 years ago

Max Alekseyev gravatar image

updated 2 years ago

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]
Preview: (hide)
link

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 ( 2 years ago )

It returns a list of Arragements elements.

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )

Oops ok , sorry.

ortollj gravatar imageortollj ( 2 years ago )

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: 2 years ago

Seen: 393 times

Last updated: Mar 04 '23