selenio34
selenio34

Reputation: 202

Split string and take only part of it (python)

QUESTION

I have a list of strings, let's call it input_list, and every string in this list is made of five words divided only by a "%" character, like

"<word1>%<word2>%<word3>%<word4>%<word5>"

My goal is, for every element of input_list to make a string made only by <word3> and <word4> divided by the "%" sign, like this "<word3>%<word4>", and create a new list made by these strings. So for example, if:

input_list = ['the%quick%brown%fox%jumps', 'over%the%lazy%dog%and']

then the new list will look like this

new_list = ['brown%fox', 'lazy%dog']

IMPORTANT NOTES AND POSSIBLE ANSWERS

input_list = ['the%quick%brown%fox%jumps', 'over%the%lazy%dog%and']
new_list = []
for element in input_list:
    current_list = element.split('%')
    final_element = [current_list[2], current_list[3]]
    new_list.append(final_element)

EDIT:

I tried to compare the running time of @Pac0 answer with the running time of @bb1 answer, and, with an input list of 100 strings, @Pac0 has a running time of 92.28286 seconds, @bb1 has a running time of 42.6106374 seconds. So I will consider @bb1 one as the answer.

Upvotes: 1

Views: 1811

Answers (3)

Pac0
Pac0

Reputation: 23149

You can use a regular expression (regex) with a capture group:

import re

pattern = re.compile('[^%]*%[^%]*%([^%]*%[^%]*)%[^%]*')
input_list = ['the%quick%brown%fox%jumps', 'over%the%lazy%dog%and']

result = [pattern.search(s).group(1) for s in input_list]
print(result)

Note: the "compile" part is not strictly needed, but can help performance if you have a lot of strings to process.

Upvotes: 2

Divyanshu Srivastava
Divyanshu Srivastava

Reputation: 1507

How about this?

input_list = ['the%quick%brown%fox%jumps', 'over%the%lazy%dog%and']
new_list = ['%'.join(x.split('%')[2:4]) for x in input_list]
print (new_list)

Output

['brown%fox', 'lazy%dog']

Upvotes: 1

bb1
bb1

Reputation: 7863

new_list = ['%'.join(w.split('%')[2:4]) for w in input_list]

Upvotes: 6

Related Questions