0

So this is my array:

field = [[1, 2, 4, 4],
         [4, 1, 4, 2],
         [2, 1, 4, 3],
         [2, 4, 2, 3],
         [1, 2, 3, 4]]

and if i have following coordinates:

co= [(0, 1),
     (1, 1),
     (2, 1)]

In this case my new array should look like:

[[1, 4, 4],
 [4, 4, 2],
 [2, 4, 3],
 [2, 4, 2, 3],
 [1, 2, 3, 4]]

anyone has any idea how ? I have managed to get just numbers that have to be deleted but its not what i want.. Note: It has to be made for field array of x length and same with coordinates.

Thanks for help!

8
  • 1
    for (x, y) in co: del field[x][y] (that would be python) Commented Dec 30, 2014 at 16:57
  • What exactly do the coordinates do? Specifically, what is the second number in the coordinate doing? Commented Dec 30, 2014 at 16:57
  • for (x, y) in co: del field[x][y] print(field) Gives : IndexError: list assignment index out of range Commented Dec 30, 2014 at 17:29
  • first number gives which row it is and the 2nd one is which column in that row So if it is 0,1 the number would be 2 Commented Dec 30, 2014 at 17:36
  • @njzk2, only need to add reverse-sort and it's perfect :) Commented Dec 30, 2014 at 17:45

1 Answer 1

1

Use this:

for c in sorted(co, reverse=True):
    field[c[0]].pop(c[1])

Or as @njzk2 suggested:

for (x, y) in sorted(co, reverse=True): del field[x][y]

Simply access the field list-of-lists in index c[0], which indicates what list is to be accessed.

Then pop the element from the sub-list in index c[1].

Result is now in field.

Note: I sort them in reversed order for the case you'll remove from the same list, so indices for previous elements don't change. For instance: (0, 3) should pop before (0, 1).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.