Gabriel
Gabriel

Reputation: 42329

Split string when a certain character is read

I have several strings stored in a file one per line like this:

dsfsdfsd/mhgjghj
cvcv/xcvxc
werwr/erewrwer
nbmbn/iuouiouio
...

As you can see the only character that is always present is the backlash /, the rest being pretty random in its composition. I need to store the first and second part (ie: before and after the backlash respectively) of each line separately, so as to end up with something like this:

first_list = [dsfsdfsd, cvcv, werwr, nbmbn, ...]
secnd_list = [mhgjghj, xcvxc, erewrwer, iuouiouio, ...]

I could do this in python iterating through each line, checking for the existence of the backlash and storing the contents of each part of the line separately. It would look like this:

first_list, secnd_list = [], []
for line in file:
    for indx, char in enumerate(line):
        if char == '/':
            first_list.append(line[:(indx-1)])
            secnd_list.append(line[(indx-1):])
            break

I'm looking for a prettier (more pythonic) version of this code.

Upvotes: 1

Views: 137

Answers (2)

rlms
rlms

Reputation: 11060

As well as str.split you could use str.partition:

first_parts = []
second_parts = []
for line in file:
    before, _, after = line.partition('/')
    first_parts.append(before)
    second_parts.append(after)

An alternative more functional oneliner:

first_parts, _, second_parts = zip(*(line.partition('/') for line in file))

Explanation for the _ in both options - str.partition returns a tuple: (first_part, seperator, last_part). Here, we don't need the seperator (indeed I can't imagine why you ever would), so we assign it to the throwaway variable _.

Here are the docs for str.partition, and here are the docs for str.split.

Upvotes: 3

Maciej Gol
Maciej Gol

Reputation: 15854

split() might come in handy here:

first_list, secnd_list = [], []
for line in file:
    first, second = line.split('/')
    first_list.append(first)
    secnd_list.append(second)

One of the assumptions made here is that only a single / is present. Knowning that, split('/') will always return a 2-tuple of elements. If this assumption is false, try split('/', 1) instead - it limits the number of splits to 1, counting left-to-right.

Upvotes: 6

Related Questions