Divide the list into other sublists, the splitting will be based on the space defined in the main list

Let's say I have this list:

list1 = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] 

I want to end up with:

 sublist1 = ["I", "am", "happy"] sublist2 = ["and", "fine"] sublist3 = ["and", "good"] 

So, I want to split the list into sub-lists based on the space that is there in list1 .

+4
source share
6 answers

itertools.groupby is the ideal weapon for this, using the str.isspace property to separate groups and filter groups with space.

 import itertools list1 = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] result = [list(v) for k,v in itertools.groupby(list1,key=str.isspace) if not k] print(result) 

result:

 [['I', 'am', 'happy'], ['and', 'fine'], ['and', 'good']] 

If you know that there are 3 variables (which is not very wise), you can unpack

 sublist1,sublist2,sublist3 = result 

but it’s better to save the result as a list of lists.

+7
source

You can do this using the for loop, placing the received signatures in the dictionary (as opposed to creating variables on the fly):

 lst = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] count = 1 dct = {} for x in lst: if x.isspace(): count += 1 continue dct.setdefault('sublist{}'.format(count), []).append(x) print(dct) # {'sublist2': ['and', 'fine'], # 'sublist3': ['and', 'good'], # 'sublist1': ['I', 'am', 'happy']} 
+4
source

Well, you can use the itertools module to group elements according to whether they are spaces or not.

For example, you can use the str.ispace function as a predicate to group elements:

 list1 = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] for key, group in itertools.groupby(list1, key=str.isspace): print(key, list(group)) 

You are getting:

 False ['I', 'am', 'happy'] True [' '] False ['and', 'fine'] True [' '] False ['and', 'good'] 

Based on this, you can create a list by excluding groups whose key is True ( isspace returned True ):

 result = [list(group) for key, group in itertools.groupby(list1, key=str.isspace) if not key] print(result) 

You get a list of lists:

 [['I', 'am', 'happy'], ['and', 'fine'], ['and', 'good']] 

If you are not familiar with understanding lists, you can use a loop:

 result = [] for key, group in itertools.groupby(list1, key=str.isspace): if not key: result.append(list(group)) 

You can unpack this result into 3 variables:

 sublist1, sublist2, sublist3 = result 
+4
source

is there anything related to str.isspace, but for a new line, i.e. instead of a space in the list will be "\ n"?

str.join + re.split() solution in an extended example:

 import re list1 = ["I", "am", "happy", " ", "and", "fine", "\n", "and", "good"] result = [i.split(',') for i in re.split(r',?\s+,?', ','.join(list1))] print(result) 

Output:

 [['I', 'am', 'happy'], ['and', 'fine'], ['and', 'good']] 
+2
source

A simple answer to your problem:

 list1 = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] new_list = [] final_list = [] list1.append(" ") # append an empty str at the end to avoid the other condn for line in list1: if (line != " "): new_list.append(line) # add the element to each of your chunk else: final_list.append(new_list) # append chunk new_list = [] # reset chunk sublist1,sublist2, sublist3 = final_list print sublist1,sublist2, sublist3 
0
source

Just for fun. If you know that words do not have a place, you can select a special character (for example, "&") to combine and split the lines:

 >>> l = ["I", "am", "happy", " ", "and", "fine", " ", "and", "good"] >>> '&'.join(l) 'I&am&happy& &and&fine& &and&good' >>> '&'.join(l).split(' ') ['I&am&happy&', '&and&fine&', '&and&good'] >>> [[w for w in s.split('&') if w] for s in '&'.join(l).split(' ')] [['I', 'am', 'happy'], ['and', 'fine'], ['and', 'good']] 

If you want the most reliable solution, select groupby .

0
source

Source: https://habr.com/ru/post/1271873/


All Articles