0

This an example of a possible list with the sublists:

  rows = [[1, 1, 'x'], [1, 'x', 1], [1, 1, 'x'], [1, 'x', 1], ['x', 1, 1], ['x', 1, 1]]

I am looking for an efficient way to replace 'x' either by 0 or 1 in the sublists. The sublists has no fixed sized or number. I mean, it could be a list like this one:

    [(1, 1, 'x', 'x'), (1, 1, 'x', 'x'), (1, 'x', 1, 'x'), (1, 'x', 'x', 1)]

I tried this:

  for row in rows:
      for pos in row:
        if pos == 'x':
           pos.replace('x','0')

Even knowning that I need '0' as an integer, just to print an output. But rows kept the 'x' unchanged.

Any suggestions?

1
  • Strings are immutable, replace returns a new string Commented Oct 8, 2019 at 12:46

4 Answers 4

2

You could just use a list comprehension and check the value and replace that like,

>> rows = [[1, 1, 'x'], [1, 'x', 1], [1, 1, 'x'], [1, 'x', 1], ['x', 1, 1], ['x', 1, 1]]
>>> [[0 if y == 'x' else y  for y in x] for x in rows] # replace `x` with 0
[[1, 1, 0], [1, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 1, 1]]

>>> another = [(1, 1, 'x', 'x'), (1, 1, 'x', 'x'), (1, 'x', 1, 'x'), (1, 'x', 'x', 1)]
>>> [[5 if y == 'x' else y  for y in x] for x in another] # replace `x` with 5
[[1, 1, 5, 5], [1, 1, 5, 5], [1, 5, 1, 5], [1, 5, 5, 1]]
Sign up to request clarification or add additional context in comments.

Comments

2

Try this code:

rows = [[1, 1, 'x'], [1, 'x', 1], [1, 1, 'x'], [1, 'x', 1], ['x', 1, 1], ['x', 1, 1]]

temp_dic = {'x':0}
modified_list  = map(
  lambda sub_list: sub_list if 'x' not in sub_list else [temp_dic.get(n, n) for n in sub_list],
  rows)
print(list(modified_list))

Output:

[[1, 1, 0], [1, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 1, 1]]

Basically, we are iterating through the sublist and checking if a value (0) is present for the key (x). If yes, replace it, otherwise, keep the key.

dic.get(n, n) returns the value(0) if it exists or it simply just returns key (e.g numbers in our case). This way we only replace the 'x' not the numbers.

2 Comments

When I run the code I got this: NameError: name 'dic' is not defined
Fixed it! It was a variable name typo. try now
1
rows = [[1, 1, 'x'], [1, 'x', 1], [1, 1, 'x'], [1, 'x', 1], ['x', 1, 1], ['x', 1, 1]]

for r in rows:
    for i, pos in enumerate(r):
        if pos == 'x':
            r[i] = 0
print(rows)

#output replace x with 0: [[1, 1, 0], [1, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 1, 1]]
#output replace x with 99: [[1, 1, 99], [1, 99, 1], [1, 1, 99], [1, 99, 1], [99, 1, 1], [99, 1, 1]]

This works well for me on Python 3.7.4. Cheers and good luck!

Comments

0

If you still wanted to use the replace method, you would need to append the values to a new list after replacing the string:

rows=[(1, 1, 'x', 'x'), (1, 1, 'x', 'x'), (1, 'x', 1, 'x'), (1, 'x', 'x', 1)]

new_list=[]
for row in rows:
    sublist=[]
    for pos in row:
        if pos == 'x':
            pos=pos.replace('x','0')
        sublist.append(pos)
    new_list.append(sublist)

print(new_list)
# [[1, 1, '0', '0'], [1, 1, '0', '0'], [1, '0', 1, '0'], [1, '0', '0', 1]]

1 Comment

As I said, I need to keep the '0's or '1's as integers. Your solution is not good for me in this particular case. Thanks, anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.