简体   繁体   中英

How to remove from a list the strings that have less than N tokens?

Given a list of strings, say:

a = ['hey','hey how are you','good how are you','I am', 'I am fine 8998','9809 908']

How can I remove the strings that have less than three tokens?:

a = ['hey how are you','good how are you', 'I am fine 8998']

I tried to:

' '.join(a.split(' ')[3:])

However, its not working. Any idea of how to remove all the strings that have less than three tokens

You could use this list comprehension:

>>> [i for i in a if len(i.split())>=3]
['hey how are you', 'good how are you', 'I am fine 8998']

This is another way of doing the same using filter and lambda :

a = ['hey','hey how are you','good how are you','I am', 'I am fine 8998','9809 908']

res = list(filter(lambda x: x.count(' ') >= 2, a))
# ['hey how are you', 'good how are you', 'I am fine 8998']

Or:

res = list(filter(lambda x: len(x.split()) >= 3, a))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM