简体   繁体   中英

Python split a list into several lists for each new line character

I have the following list, and I would like to split it into several lists when the element in the list is "\n".

Input:

['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290\n', '27\n','\n','chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064\n','27\n','\n']

expected output:

[
 ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290', '27'],
 ['chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064', '27']
]

I tried stripping the elements with "\n" at the end of them and used and modified the accepted answer from this post :

for i, n in enumerate(lst):
    if n != "\n":
        lst[i] = lst[i].rstrip("\n")

[item.split(",") for item in ','.join(lst).split('\n') if item]

But since I am using a comma instead of a single white space to join and split, I get "" after splitting into several lists. How can I prevent this?

[
 ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290','27',''],
 ['','chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064','27','']
]

This work for you?

list1 = ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290\n', '27\n','\n','chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064\n','27\n','\n']

list2 = []
tmp = []
for item in list1:
    if item != '\n':
        tmp.append(item.rstrip('\n'))
    else:
        #Note we aren't actually processing this item of the input list, as '\n' by itself is unwanted
        list2.append(tmp)
        tmp = []

I would recommend splitting your list with more_itertools.split_at .

Because your original list ends with the separator, '\n' , splitting it will result in the final item of your list being an empty sublist. The if check excludes this.

from more_itertools import split_at

original = [
    'chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290\n',
    '27\n',
    '\n',
    'chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064\n',
    '27\n',
    '\n'
]

processed = [
    [item.rstrip() for item in sublist]
    for sublist in split_at(original, lambda i: i == '\n')
    if sublist
]

print(processed)

Output (line break added for clarity):

[['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290', '27'],
 ['chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064', '27']]

Use a list comprehension to strip the \n item from the list and use .strip() to remove the \n at the end of the lines you want to keep. Then loop through the temp list ( blist ) and create your final list.

alist = ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290\n', '27\n','\n','chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064\n','27\n','\n']

# use line comprehension to remove the \n from the list
# and use the .strip() to remove the trailing \n in the strings
blist = [i.strip() for i in alist if i != '\n']

final_list = []
for i in range(0,len(blist),2):
    final_list.append( [blist[i], blist[i+1]] )

You could use groupby :

from itertools import groupby

seq = ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290\n', '27\n','\n','chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064\n','27\n','\n']

result = [
    [string.rstrip() for string in group]
    for key, group in groupby(seq, lambda s: s != "\n")
    if key
]

Result:

[
    ['chain 2109 chrY 59373566 + 1266734 1266761 chrX 156040895 + 1198245 1198272 20769290', '27'],
    ['chain 2032 chrY 59373566 + 1136192 1136219 chrX 156040895 + 1086629 1086656 4047064', '27']
]

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