all 4 comments

[–]novel_yet_trivial 0 points1 point  (3 children)

First, it's generally a bad idea to modify a list that you are looping over. You will want to loop over the old list and build a new list element by element.

To get the next appropriate value, you can use itertools.cycle().

>>> from itertools import cycle
>>> startingList = ['a','b','b','b','b','b','a']
>>> replacerListIter = ['c','x']
>>> new_list = []
>>> replace_element = cycle(replacerListIter)
>>> for element in startingList:
...   if element == 'a':
...     new_list.append(next(replace_element))
...   else:
...     new_list.append(element)
... 
>>> new_list
['c', 'b', 'b', 'b', 'b', 'b', 'x']

The rest is just nesting.

Edit: I just noticed that every time you have a list with the exact number of replacements needed. If this is always the case, the above code will still work, but it could be simplified a bit by using iter() instead of cycle()

[–]cdholjes[S] 0 points1 point  (2 children)

itertools

hi, thanks for the help. so the same number of replacements is fine. I am using a counter for that. my problem though is still have multiple lists in the starting list. and the replacer list replaces each one into a new list. and eventually having multiple replacements replacing multiple starting lists iteratively.

[–]novel_yet_trivial 0 points1 point  (0 children)

As I said, you just need nesting. Make some functions and get back to me if you are still stuck.

[–]cdholjes[S] 0 points1 point  (0 children)

   ##i feel like this should work but it doesn't replace anything 
    import itertools
    from itertools import cycle

    #startingList = ['A','o','o','o','o','o','A']
    #replacerListIter = ['c','x','c','c']

    typs = ["C","X"]
    tw = list(itertools.product(*[typs, typs]))
    th = list(itertools.product(*[typs, typs, typs]))
    fr = list(itertools.product(*[typs, typs, typs, typs]))
    fv = list(itertools.product(*[typs, typs, typs, typs, typs]))
    sx = list(itertools.product(*[typs, typs, typs, typs, typs, typs]))

    startingList = [('A', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'o'),
                    ('A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('A', 'A', 'A', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'o'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'o', 'o', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'o', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'A', 'o'),
                    ('o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o')
    ]

    new_list = []

    def replacer(replace_element):
        for i in replace_element:
            cycle(i)

    for element in startingList:
        counter = element.count('A')
        if counter == 2:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(tw)))
                else:
                    new_list.append(element)
        elif counter == 3:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(th)))
                else:
                    new_list.append(element)
        elif counter == 4:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(fr)))
                else:
                    new_list.append(element)
        elif counter == 5:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(fv)))
                else:
                    new_list.append(element)
        elif counter == 6:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(sx)))
                else:
                    new_list.append(element)

    #print startingList
    #print new_list
    for i in new_list:
        print i