Wednesday, May 24, 2017

Deleting multiple elements of a list by index

Deleting multiple elements of a list,
Filter a list by list of index, either keep or remove  elements.
Selection may well be the result of search(My_search_term,List)
or something else.


Usage:



List=[12,3,4,5,6,7,8,90,1 ]; Selection=[0,3,4,5]; 

echo(" List :",List);
echo(" Indices of elements to remove :",Selection);
/*Indexes and indices are both accepted and 
widely used plurals of the noun index. */
echo(" List after removal :",split_keepers(List,Selection));
echo(" List of removed elements :", split_removed(List,Selection));

 
function split_keepers(List,Selection)=
        [for(i=[0:max(0,len(List)-1)]) if(search(i,Selection)==[])List[i]];
function split_removed(List,Selection)=
        [for(i=[0:max(0,len(List)-1)]) if(search(i,Selection)!=[])List[i]]; 


No comments:

Post a Comment