How Can You Efficiently Remove Multiple Rows from a List by ID?

  • Thread starter Thread starter raymound
  • Start date Start date
AI Thread Summary
To remove specific rows from a list based on their IDs without altering the IDs during the process, the recommended approach is to iterate through the list in reverse order. By starting with the highest ID and working downwards, the indices of the remaining rows do not change as rows are dropped. This method ensures that each specified row is successfully removed without affecting the IDs of the rows that are yet to be processed. This technique is particularly useful when dealing with lists where the row indices are dynamically changing due to deletions.
raymound
Messages
13
Reaction score
0
hi
I have a list which I want to drop some of it's rows
and I have the id of the rows I want to drop in a list and there's no algorythem in the list

how can I do that?
I know the Command Drop[MC, {id}] whould drop the row number id but if I put this in a loop and try to drop them one by one the id's whould change since I'm dropping some rows and I tried to figure a way out of it but the just didn't work, any help?

the first one in my list and the second one the id of the rows to be dropped

PHP:
{{0.00414938, 6.}, {0.00438052, 5.9}, {0.00460842, 5.8}, {0.00482298, 
  5.7}, {0.0050097, 5.6}, {0.00515407, 5.5}, {0.00517174, 
  5.4}, {0.00504864, 5.3}, {0.00469823, 5.2}, {0.00402235, 
  5.1}, {0.0029449, 5.}, {0.00165498, 4.9}, {-0.00110326, 
  4.8}, {-0.00556929, 4.7}, {-0.0113029, 4.6}, {-0.0183193, 
  4.5}, {-0.0258924, 4.4}, {-0.0363815, 4.3}, {-0.0540341, 
  4.2}, {-0.0762913, 4.1}, {-0.0978172, 4.}, {-0.120518, 
  3.9}, {-0.15479, 3.8}, {-0.188365, 3.7}, {-0.221846, 
  3.6}, {-0.250625, 3.5}, {-0.283568, 3.4}, {-0.276114, 
  3.3}, {-0.227546, 3.2}, {-0.129161, 3.1}, {0.0897719, 
  3.}, {0.511911, 2.9}, {1.19876, 2.8}, {2.32358, 2.7}, {4.16475, 
  2.6}, {7.02193, 2.5}, {11.4346, 2.4}, {18.2226, 2.3}, {28.5117, 
  2.2}, {44.0589, 2.1}, {67.3399, 2.}, {"", ""}}

PHP:
{2, 3, 4, 5, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 22, 24, 26,
28, 30, 32, 34, 36, 38, 40}
 
Physics news on Phys.org
raymound said:
if I put this in a loop and try to drop them one by one the id's would change since I'm dropping some rows

What if your loop drops them one by one starting with the end of your list instead of the beginning?

First drop row 40 and then drop row 38 and then drop row 36 ... and then drop row 2.

The number of rows remaining in your list would decrease with each iteration, but by doing this in reverse order of the id's that you still need to delete would not change.
 

Similar threads

Back
Top