+ 3

Sentence lengths

I have put a full texts into sentences, each sentence in an array. In python, how can i get rid of the sentences above 20 words?

27th Feb 2021, 9:53 PM
Ash 07
Ash 07 - avatar
7 Réponses
+ 3
Shoo sortai answer is right if right if your text structure is like: sentences = [ ['word', 'word', ... ], ['word', 'word', ... ], ... ] is it the form of your data? or did we miss something?
27th Feb 2021, 11:07 PM
visph
visph - avatar
+ 3
:D now Shoo sortai answer is right if your structure is like: sentences = [ 'sentence of multiple words', ... ] I guess that the first was good, but I could be wrong: could you detail wich structure you're using?
27th Feb 2021, 11:18 PM
visph
visph - avatar
+ 3
Ash 07 What you're describing sounds like you should use the filter() function. The filter function takes a function as its first argument and the iterable as its second. Each item of the iterable is passed to the function argument and if that function returns True then the item is added to the returned list from the filter function. The first argument function is usually a lambda. So your filter function would look something like; new_list = filter(lambda x: len(x) <= 20, old_list) This would keep only the items from the old_list that had a length of 20 or less in the new_list.
28th Feb 2021, 12:34 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
exactly I understand that your structure is: sentence1 = ['word','word',...] sentence2 = ['word','word',...] ... in wich case you should rather use a list of list ;)
27th Feb 2021, 11:21 PM
visph
visph - avatar
+ 1
you can use a list comprehension: arr = [s for s in arr if len(s.split(" "))<=20]
27th Feb 2021, 10:49 PM
Shoo sortai
Shoo sortai - avatar
+ 1
visph thank you, I got a bit wrong: it should be len(s.split(" "))
27th Feb 2021, 11:11 PM
Shoo sortai
Shoo sortai - avatar
+ 1
Ash 07 I hope it works 😊 new_list = [ ] for sentence in list: words = sentence.split() if len(words) <= 20: new_list.append(sentence)
28th Feb 2021, 1:05 AM
Hasan Hüseyin Semiz
Hasan Hüseyin Semiz - avatar