+ 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?
7 Respostas
+ 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?
+ 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?
+ 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.
+ 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 ;)
+ 1
you can use a list comprehension:
arr = [s for s in arr if len(s.split(" "))<=20]
+ 1
visph thank you, I got a bit wrong: it should be len(s.split(" "))
+ 1
Ash 07
I hope it works 😊
new_list = [ ]
for sentence in list:
words = sentence.split()
if len(words) <= 20:
new_list.append(sentence)