user3682157
user3682157

Reputation: 1695

Remove any phrase in a list based on capitalization (Python 3)

I have a pandas dataframe that looks like this

   List
   Justin Bieber
   The hills
   George Clooney
   very good
   is a

Is there a fast way to filter this list so only bigrams where both words are uppercase are left? Based on the example above, the new list would look like:

  List
  Justin Bieber
  George Clooney

Upvotes: 0

Views: 29

Answers (2)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Maybe using filter?

>>> lst = ["Something", "This no", "This Yes", "not this"]
>>> list(filter(lambda s: all(x[0].isupper() for x in s.split()), lst))
['Something', 'This Yes']

With list comprehension it's pretty much the same:

>>> [s for s in lst if all(x[0].isupper() for x in s.split())]
['Something', 'This Yes']

You may want to set the number of words it matches, but your example didn't - so neither did I.

Upvotes: 1

pzelasko
pzelasko

Reputation: 2322

A one liner (but not the fastest):

names = [s for s in str_list if len(s.split()) == 2 and all(word[0].isupper() for word in s.split())]

Upvotes: 0

Related Questions