Issue
I have a list which has str
, int
, and float
elements.
I want to create sublists (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 I can cluster in more advanced ways, such as itertools.groupby
or filter
or any other advanced methods?
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']]
Answered By - enzo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.