How to Split Elements of a List

How to split elements of a list?

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']

How to split elements in list?

If you have a small list you can try this:

lst = ['Name0, Location0', 'Phone number0', 'Name1, Location1', 'Phone number1']
s = ", "
joined = s.join(lst)
newList = joined.split(s)
print(newList)

Output:

['Name0', 'Location0', 'Phone number0', 'Name1', 'Location1', 'Phone number1']

How to split items in list?

Try this code maybe:

import re
list = ['text \n\n more text (1) \n\n even more text \n\n']
list[0] = list[0].replace(' \n\n ', '#').replace(' \n\n', '#')
list = re.split('#',list[0])

if list[len(list) - 1] == '':
list.pop(len(list) - 1)

print(list)

Output:

['text', 'more text (1)', 'even more text']

First we replace every instance of ' \n\n ' and ' \n\n' with '#'. This is because even though the elements are separated by ' \n\n ', the code ends without a space after it, so we need a unique separator for that instance.

Afterwards, we split the list by every instance of '#', and pop the final element if it was a black space caused by an ending ' \n\n ' or ' \n\n '.

I hope this helped! Please let me know if you need any further clarification or details :)

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

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']]

How split an element and replace both part in list with python

Assuming you're happy with creating a new list, one way to achieve that is to join the existing list together with spaces and then split on either a space or a ':

import re

mylist = [
'La', 'domestication', "d'un",
'animal', 'ou', "d'un", 'végétal,',
'necessite', "l'acquisition",
"d'une", 'ferme'
]

my_new_list = re.split(r" |(?<=')", ' '.join(mylist))

Output:

[
'La', 'domestication', "d'", 'un',
'animal', 'ou', "d'", 'un', 'végétal,',
'necessite', "l'", 'acquisition',
"d'", 'une', 'ferme'
]

Note this assumes the words in the list don't have a space in them; if they might, you can just replace the space in the code with a character (or character sequence) which does not occur in the words, e.g. \0:

my_new_list = re.split(r"\0|(?<=')", '\0'.join(mylist))


Related Topics



Leave a reply



Submit