简体   繁体   中英

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM