1

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.

3
  • I also need this for a general case, not specifically for this case. Commented Jun 23, 2016 at 15:18
  • 1
    you can use regular expressions Commented Jun 23, 2016 at 15:20
  • @BenjaGarrido Yes, They are the easiest to understand but unfortunately they are slower. Commented Jun 23, 2016 at 15:49

2 Answers 2

3

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']
Sign up to request clarification or add additional context in comments.

6 Comments

haha I didn't see your list comprehension before I submitted my answer. +1 for the two solutions.
@Jeremy Thanks, I would not have edited if I saw your answer. :)
can someone explain exactly what is happening in Rao's list comprehension answer. he's joining then splitting at '-' and '.' ?
@mariano Done, Please take a look and inform me if you still have any queries
Why couldn't I have used the reverse function instead?
|
2

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

1 Comment

Pro-tip, Always explain outside the code blocks. The CSS for comments is too faded to meet the eye.

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.