Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cluster list elements into sub-lists if an element is greater than threshold

I have a list which has str , int and float elements.
I want to create sub-lists (cluster elements) in such a way that if any numeric element is greater than threshold (here threshold is 3), a cluster will be created containing the previous elements. Code and expected output is given below.

L = ["this is", "my", 1, "first line", 4, "however this", 3.5 , "is my last line", 4] 
out = []
temp = []
for x in L:
  if isinstance(x, str) or x <3:
    temp.append(x)
  else:
    out.append(temp)
    temp = []
  
print(out)
[['this is', 'my', 1, 'first line'], ['however this'], ['is my last line']]

I want to check how can I can cluster in more advanced way such as itertools.groupby or filter or any other advanced methods ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use itertools.groupby using key as the predicate:

import itertools

L = ["this is", "my", 1, "first line", 4, "however this", 3.5 , "is my last line", 4] 

print([
    list(values)  # Convert the groups as a list
    for key, values in itertools.groupby(L, key=lambda x: isinstance(x, str) or x < 3)
    if key        # If the predicate is true
])

It outputs

[['this is', 'my', 1, 'first line'], ['however this'], ['is my last line']]

and how can I remove int/float elements with same groupby method

Use a nested list comprehension:

import itertools

L = ["this is", "my", 1, "first line", 4, "however this", 3.5 , "is my last line", 4] 

print([
    [value for value in values if not isinstance(value, (int, float))]
    for key, values in itertools.groupby(L, key=lambda x: isinstance(x, str) or x < 3)
    if key
])

It outputs

[['this is', 'my', 'first line'], ['however this'], ['is my last line']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading