简体   繁体   中英

Loop through nested lists and remove a certain element

All I want to do is loop through a nested list (eg [[2],1,[4,[]]] ) and remove an element if it fits the condition I set it for it.

eg:

  • if the element is an int -> remove
  • if the element is any empty list -> remove

I would appreciate any help I get with examples provided so i can understand. Would prefer not to use libraries.

Thank you so much for reading this far!

Question has been edited as the code gives different errors with different inputs.

For reference here is the code I used but I'm getting different error now so it might not be relevant:

for i in range(len(l)): for j in range(len(l)): if l[i][j] == []: l.remove(l[i][j])

Here is how we manages a similar issue we had an ArrayList of correct spelled words this is the ArrayList capture and we wanted to compare this list to a String[] strArray that had all the words correct spelling and misspelled words. So we removed all the correct spelled words and put the misspelled word in the ArrayList list then just added these back to the String[] strArray
I would like to take credit for this code but it belongs to a fellow named Chris Jester-Young here is a link to his SO Post look at the code under Edit3
Edit3

Here is our implementation of the code

    for(int P = 0; P < capture.size();P++){

    String gotIT = capture.get(P);     
    String[] EMPTY_STRING_ARRAY = new String[0];
    List<String> list = new ArrayList<>();
    Collections.addAll(list, strArray);
    list.removeAll(Arrays.asList(gotIT));
    strArray = list.toArray(EMPTY_STRING_ARRAY); 
    // Code above removes the correct spelled words from ArrayList capture
    // Leaving the misspelled words in strArray then below they are added 
    // to the cboMisspelledWord which then holds all the misspelled words
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM