mariano mariano - 6 months ago 27
Python Question

How do I replace multiple substrings in a Python list?

I need to turn the input_string into the comment below using a for loop. First I sliced it using the split function, but now I need to somehow turn the input string into

['result1','result2', 'result3', 'result5']
. I tried replacing the .xls and the dash for nothing (') but the string output is unchanged. Please don't import anything, I'm trying to do this with functions and loops only.

input_string = "01-result.xls,2-result.xls,03-result.xls,05-result.xls"
# Must be turned into ['result1','result2', 'result3', 'result5']

splitted = input_string.split(',')

for c in ['.xls','-', '0' ]:
if c in splitted:
splitted = splitted.replace(splitted,'c','')

when I type splitted, the output is
['01-result.xls', '2-result.xls', '03-result.xls', '05-result.xls']
therefore nothing is happening.


Use the re module's sub function and split.

>>> input_string = "01-result.xls,2-result.xls,03-result.xls,05-result.xls" 
>>> import re
>>> re.sub(r'(\d+)-(\w+)\.xls',r'\2\1',input_string)
>>> re.sub(r'(\d+)-(\w+)\.xls',r'\2\1',input_string).split(',')
['result01', 'result2', 'result03', 'result05']

Using no imports, you can use a list comprehension

>>> [''.join(x.split('.')[0].split('-')[::-1]) for x in input_string.split(',')]
['result01', 'result2', 'result03', 'result05']

The algo here is, we loop through the string after splitting it on ,. Now we split the individual words on . and the first element of these on -. We now have the number and the words, which we can easily join.

Complete explanation of the list comp answer -

To understand what a list comprehension is, Read What does "list comprehension" in Python mean? How does it work and how can I use it?

Coming to the answer,

Splitting the input list on ,, gives us the list of individual file names

>>> input_string.split(',')
['01-result.xls', '2-result.xls', '03-result.xls', '05-result.xls']

Now using the list comprehension construct, we can iterate through this,

>>> [i for i in input_string.split(',')]
['01-result.xls', '2-result.xls', '03-result.xls', '05-result.xls']

As we need only the file name and not the extension, we split by using . and take the first value.

>>> [i.split('.')[0] for i in input_string.split(',')]
['01-result', '2-result', '03-result', '05-result']

Now again, what we need is the number and the name as two parts. So we again split by -

>>> [i.split('.')[0].split('-') for i in input_string.split(',')]
[['01', 'result'], ['2', 'result'], ['03', 'result'], ['05', 'result']]

Now we have the [number, name] in a list, However the format that we need is "namenumber". Hence we have two options

  • Concat them like i.split('.')[0].split('-')[1]+i.split('.')[0].split('-')[0]. This is an unnecessarily long way
  • Reverse them and join. We can use slices to reverse a list (See How can I reverse a list in python?) and str.join to join like ''.join(x.split('.')[0].split('-')[::-1]).

So we get our final list comprehension

>>> [''.join(x.split('.')[0].split('-')[::-1]) for x in input_string.split(',')]
['result01', 'result2', 'result03', 'result05']