Pop() function in python


#1

When using the pop function in python with index = _.indexOf(items,item) it seems that it does not work and removes the last element. For example:

items = base.getItems()
peasants = base.getByType('peasant')

for peasant in peasants:
    item = peasant.getNearest(items)
    if (item):
        index = _.indexOf(items,item)
        poped = items.pop(index)
        base.command(peasant, 'move', poped.pos)

The poped item returns always the last element. Is it a bug or i am doing something wrong?

Thanks in advance


#2

the python parser is not always accurate, still has much work to do.

This is a site where you can try out basic python code and see if the python parser actually deals with them properly:
https://rawgit.com/differentmatt/filbert/master/test/interactive.html

And here is where you can file an issue if something is not properly implemented:

I’ll start an issue for this one.


#3

hmmm, just checked on rawgit and the pop seems to be getting the right thing.

Since you know what item you are going for, couldn’t you just use

items.remove(item)
base.command(peasant,'move',item.pos)

That said, I haven’t used the _.indexOf function before, not sure what that does, but it doesn’t look like it is implemented in filbert. It gives a reference error.

I did not start an issue after all because it is dealing with something that I don’t understand!


#4

It returns the index of the “item” in the array of the “items”. Since it wasn`t documented that i could use the pop function in this way i tried to find a way to get the index of the item in the array. (i find it from here: http://discourse.codecombat.com/t/getnearest-result-cant-be-used-with-indexof/819/2)

I am guessing that you mean items.pop(item) . Still the same error. It removes the last element.

What i am trying to achieve is to remove the “item” from the array of the “items”, so the next peasant won`t go to the same item in the greed level. So, if i am not wrong, the item variable and the poped variable should be equal, but that is not the case.

Thanks in advance


#5

No, I meant remove.
.remove looks for an element of the list and removes it
.pop looks for an index and removes the element at that position

The problem with that link is the _.indexOf is for javascript, not for python

a=[7,2,9,4,3,6,3,9]
a.remove(3)    # this removes the element 3 from the list in the first place that it occurs
print(a)  # output is [7,2,9,4,6,3,9]

Not sure if .remove will work with objects for python or not.


#6

if i use items.remove(item) in codecombat i get Line 20, time 0.3 :tmp34[tmp35] is not a function.

Sorry for that. I am new to javascript and python. In my defence, it worked and it returned the correct index.


#7

We’re still working on having the proper Python list methods always available instead of the JS array methods–sometimes we haven’t converted the values properly. The automatic conversion will solve a lot of problems when we figure it out.