How to combine elements in list to a new nested list?

Reman

I have a list 'r' like this:

[["", 1], ["this is a text line", 2], ["this is a text line", 3], ["this is a text line", 4], ["", 5], ["", 6], ["this is a text line", 7],["this is a text line", 8], ["this is a text line", 9], ["this is a text line", 10], ["", 11], ["this is a text line", 12], ["this is a text line", 13], ["this is a text line", 14], ["", 15], ["this is a text line", 16], ["this is a text line", 17], ["this is a text line", 18], ["", 19]]

To know where are my empty lines and lines with text I filter my list:

empty = [x[1] for x in r if regex.search("^\s*$", x[0])]
text = [x[1] for x in r if regex.search("\S", x[0])]

output:

empty = [1, 5, 6, 11, 15, 19]
text= [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]

What I want to do is to combine the numbers in text if they are in sequence (text[i]-text[i+1]) = +1 (in order to define the paragraphs):

finaltext = [[2, 3, 4], [7, 8, 9, 10], [12, 13, 14], [16, 17, 18]]
finaltext including empty = [[2, 3, 4, 5, 6], [7, 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]

How can I group elements in a list based on a condition?

Joe Iddon

Pure Python solution without any modules:

This can be done using modules such as with numpy and groupby, but I thought it would be call to attempt without them, just with plain Python. Here is my solution:

text = [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]
s = 0
finaltext = []
for i in range(len(text)-1):
    if text[i] + 1 != text[i+1]:
        finaltext.append(text[s:i+1])
        s = i+1

finaltext.append(text[s:])

which gives finaltext as:

[[2, 3, 4], [7, 8, 9, 10], [12, 13, 14], [16, 17, 18]]

Update

To get both of the lists (not sure why you would want to), you can use the following:

empty = [1, 5, 6, 11, 15, 19]
text = [2, 3, 4, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18]
s = 0
finaltext = []
finaltext_including_empty = []
for i in range(len(text)-1):
    if text[i] + 1 != text[i+1]:
        finaltext.append(text[s:i+1])
        finaltext_including_empty.append(list(range(text[s], text[i+1])))
        s = i+1

finaltext.append(text[s:])
finaltext_including_empty.append(list(range(text[s],max(empty[-1]+1, text[-1]+1))))

which gives finaltext the same as before and finaltext_including_empty as:

[[2, 3, 4, 5, 6], [7, 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to group the elements of a list to form a new list

Combine list elements by suffix

How to group elements of a nested list?

Python: Create a new list of all elements in nested list sorted

How to combine elements of list? (C#)

How to separate each elements of a nested list and convert the list to the list of tuples?

How to recursively combine pairs of elements from a list?

How to combine 2 list into pairs of nested list?

combine elements of a list together forming new lists

How to combine elements in a complicated list in R?

How to combine all sublist elements into one list

How to combine all strings in a nested-list?

How to combine elements from a data list in R?

How to combine all the elements of list to a dataframe in R

Combine elements of a list

How to combine elements from the list taking into account index of the list?

How to consolidate nested list elements

How to make a new nested list from an old nested list by merging all the elements at the same index together

how to separate the list into nested list ,with the average of all elements in the list?

How to combine recurring nested dictionaries in a list?

How to aggregate the elements in a nested list

list comprehension, how to append the new elements to the list?

Combine list elements to create a nested list

Combine elements of a list in R

Iterate through a nested list and pick certain elements and create a new list

How to combine specific elements from a Python list?

How to combine like elements in list?

how to convert a nested dictionary to a list, combine it with existing list and display results

How to combine elements in set and a string to a list in python?

TOP Ranking

HotTag

Archive