0

I have a list

task_list = ['A1.wakeup', 'A2.brush','B1.route','B2.breakfast']

the result i want is

task_list = ['1A1.wakeup', '1A2.brush','2B1.route','2B2.breakfast']

I use a for loop:

new_task_list = task_list[:]
for task in new_task_list:
    task.replace('A', '1A')
    task.replace('B','1B')

In [61]: new_task_list
Out[61]: ['A1.wakeup', 'A2.brush', 'B1.route', 'B2.breakfast']

It does not change, What's the problem?

0

5 Answers 5

1

str.replace does not work in-place (pun unintended).

You can create a mapping of the replacements and use a list comprehension to build the new list looking up values from the mapping an prepending those to each string:

mapping = {'A': '1', 'B': '2'}
new_list = [mapping[x[0]]+x for x in task_list]
print(new_list)
# ['1A1.wakeup', '1A2.brush', '2B1.route', '2B2.breakfast']
Sign up to request clarification or add additional context in comments.

Comments

1

Taking cue from Moses Koledoye's answer

task_list = ['A1.wakeup', 'A2.brush','B1.route','B2.breakfast']

Getting the difference of ascii value of A (=65), B (=66) and 64

[ str( ord(x[0]) - 64 )+x for x in task_list ]

# ['1A1.wakeup', '1A2.brush', '2B1.route', '2B2.breakfast']

1 Comment

This is what I thought about too +1
0

The problem is that strings in python are inmutable (they can not change), that's why replace will return a new string (the original one will be the same). To do what you want you could try:

new_task_list = task_list[:]
for idx, task in enumerate(new_task_list):
    new_task_list[idx] = new_task_list[idx].replace('A', '1A')
    new_task_list[idx] = new_task_list[idx].replace('B', '2B')

or even:

from operator import methodcaller
new_task_list = map(methodcaller('replace', 'B', '2B'),
                map(methodcaller('replace', 'A', '1A'), task_list))

2 Comments

You use a 'x' as trailing in 'idx',is it a recommended practice?
hmmm not really, the recommended practice would be to have meaningful variable names, in this case it's only the abbreviation for "index" and I think it's meaningful, but that's a bit subjective.
0
task_list = ['A1.wakeup', 'A2.brush','B1.route','B2.breakfast']
task_list = [w.replace('A', '1A') for w in task_list]
task_list = [w.replace('B', '2B') for w in task_list]

Then, you will get

task_list = ['1A1.wakeup', '1A2.brush', '2B1.route', '2B2.breakfast']

Comments

0

task in your for loop is just a variable updated on each loop iteration, task.replace('A', '1A') doesn't change the list value inplace.

I would suggest the following solution with re module:

import re

task_list = ['A1.wakeup', 'A2.brush','B1.route','B2.breakfast']
new_task_list = [ re.sub(r'^(A|B)', lambda m: ('1' if m.group(1) == 'A' else '2') + m.group(1), t)
                  for t in task_list]

print(new_task_list)

The output:

['1A1.wakeup', '1A2.brush', '2B1.route', '2B2.breakfast']

Or even simpler:

task_list = ['A1.wakeup', 'A2.brush','B1.route','B2.breakfast']
search = {'A':'1', 'B':'2'}
new_task_list = [ (search[t[0]] if t[0] in search else '') + t for t in task_list]
print(new_task_list)

Comments

Your Answer

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