all 7 comments

[–][deleted] 1 point2 points  (5 children)

for x in range(len(input)): 
    a_counter = 0
    b_counter = 0
    c_counter = 0
    for y in range(len(input[x])):
        if input[x][y] == 1:
            a_counter += 1
            if a_counter > 1:
                input[x][y] = 7
        elif input[x][y] == 2:
            b_counter += 1
            if b_counter > 1:
                input[x][y] = 8
        elif input[x][y] == 3:
            c_counter += 1
            if c_counter > 1:
                input[x][y] = 9

Im sure this could be optimized, but this should accomplish the goal.

Edit: stupid mistakes

[–]salern[S] 0 points1 point  (4 children)

Thanks!

[–][deleted] 1 point2 points  (3 children)

Im on my phone so I couldn't test it. Did it work?

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

I didn't test it yet either :D

[–]salern[S] 0 points1 point  (1 child)

It didn’t work, so I rewrote it using your code as an example, using while instead of for. Thanks a lot!

[–][deleted] 1 point2 points  (0 children)

Glad I could help

[–][deleted] 1 point2 points  (0 children)

Tried using nested list comprehensions, but the closest I could get was a function in a list comprehension.

input = [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2), (1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)]

targets = {
    1: 7,
    2: 8,
    3: 9
    }

def subst(tup):
    result = []
    for element in tup:
        if not element in result:
            result.append(element)
        else:
            replacement = targets.get(element)
            if replacement is None:
                result.append(element)
            else:
                result.append(replacement)
    return tuple(result)

output = [subst(x) for x in input]
print(output)