Python: Iterate over each item in nested-list-of-lists and replace specific items

Sandeep Kulkarni

I am a beginner in python. I want to know if there is any in-built function or other way so I can achieve below in python 2.7:

Find all -letter in list and sublist and replace it with ['not',letter]

Eg: Find all items in below list starting with - and replace them with ['not',letter]

Input : ['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']]
Output : ['and', ['or', ['not','S'], 'Q'], ['or', ['not','S'], 'R'], ['or', ['or', ['not','Q'], ['not','R']], ['not','S']]]

Can anyone suggest how to do it in python. Thanks

salparadise

Try a bit of recursion:

def change(lol):
    for index,item in enumerate(lol):
        if isinstance(item, list):
            change(item)
        elif item.startswith('-'):
            lol[index] = ['not',item.split('-')[1]]
    return lol

In action:

In [24]: change(['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']])
Out[24]:
['and',
['or', ['not', 'S'], 'Q'],
['or', ['not', 'S'], 'R'],
['or', ['or', ['not', 'Q'], ['not', 'R']], ['not', 'S']]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Iterate over nested sublists and remove specific items

From Dev

Iterate over combinations of items of multiple lists in Python

From Dev

iterate over a list of lists with another list in python

From Dev

Iterate over list using lambda to get nested lists

From Dev

Iterate over 2 nested lists

From Dev

How iterate over List<T> and render each item in JSF Facelets

From Dev

How iterate over List<T> and render each item in JSF Facelets

From Dev

JSP: Iterate over List and get each item (ForEach)

From Dev

making a list of items combining each item from several lists

From Dev

Using jQuery to find list items containing specific text in nested lists

From Dev

Iterate over two lists and combining items

From Dev

how to count the total number of items for the lists contain a specific item in python

From Dev

Django template iterate over nested lists

From Dev

How to iterate over a list once only in a nested loop in Python

From Dev

Iterate over a list in python

From Dev

Iterate over a dict except for x item items

From Dev

Iterate over specific instances in a List

From Dev

How best to iterate over a list when a each list item will be passed to a function?

From Dev

Python: iterate over a list of list

From Dev

Replacing the last item inside of some nested lists with each consecutive item of another list

From Dev

iteration over python nested lists

From Java

Iterate over all pairs of consecutive items in a list

From Dev

Can make iterate over a list of sources and give me alphabetic keys for each item?

From Dev

Summation of specific items in a nested list

From Dev

jquery to iterate through list items with nested inputs

From Dev

Java iterate over nested maps of list

From Dev

Python iterate over two lists simultaneously

From Dev

Elegant way to iterate over heads of lists in Python

From Dev

Python: Iterate over list of variables

Related Related

  1. 1

    Iterate over nested sublists and remove specific items

  2. 2

    Iterate over combinations of items of multiple lists in Python

  3. 3

    iterate over a list of lists with another list in python

  4. 4

    Iterate over list using lambda to get nested lists

  5. 5

    Iterate over 2 nested lists

  6. 6

    How iterate over List<T> and render each item in JSF Facelets

  7. 7

    How iterate over List<T> and render each item in JSF Facelets

  8. 8

    JSP: Iterate over List and get each item (ForEach)

  9. 9

    making a list of items combining each item from several lists

  10. 10

    Using jQuery to find list items containing specific text in nested lists

  11. 11

    Iterate over two lists and combining items

  12. 12

    how to count the total number of items for the lists contain a specific item in python

  13. 13

    Django template iterate over nested lists

  14. 14

    How to iterate over a list once only in a nested loop in Python

  15. 15

    Iterate over a list in python

  16. 16

    Iterate over a dict except for x item items

  17. 17

    Iterate over specific instances in a List

  18. 18

    How best to iterate over a list when a each list item will be passed to a function?

  19. 19

    Python: iterate over a list of list

  20. 20

    Replacing the last item inside of some nested lists with each consecutive item of another list

  21. 21

    iteration over python nested lists

  22. 22

    Iterate over all pairs of consecutive items in a list

  23. 23

    Can make iterate over a list of sources and give me alphabetic keys for each item?

  24. 24

    Summation of specific items in a nested list

  25. 25

    jquery to iterate through list items with nested inputs

  26. 26

    Java iterate over nested maps of list

  27. 27

    Python iterate over two lists simultaneously

  28. 28

    Elegant way to iterate over heads of lists in Python

  29. 29

    Python: Iterate over list of variables

HotTag

Archive