marjak
marjak

Reputation: 93

How to replace multiple substrings in a 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.

Upvotes: 1

Views: 176

Answers (2)

Jeremy
Jeremy

Reputation: 828

Here's a solution using list comprehension and string manipulation if you don't want to use re.

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(',')

#Remove extension, then split by hyphen, switch the two values,
#and combine them into the result string
print ["".join(i.split(".")[0].split("-")[::-1]) for i in splitted]

#Output
#['result01', 'result2', 'result03', 'result05']

The way this list comprehension works is:

  1. Take the list of results and remove the ".xls". i.split(".)[0]
  2. Split on the - and switch positions of the number and "result". .split("-")[::-1]
  3. For every item in the list, join the list into a string. "".join()

Upvotes: 2

Bhargav Rao
Bhargav Rao

Reputation: 52071

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)
'result01,result2,result03,result05'
>>> 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" 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']

Upvotes: 3

Related Questions