Python Forum
Filter out all words that begin with P, B, or T.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter out all words that begin with P, B, or T.
#3
You can combine string method .startswith and list comprehension with conditional to get desired result. This one of the most straightforward translations of spoken language into Python: "give me color for every color in colors which is not starting with letters 'P', 'B', 'T'":

>>> [color for color in colors if not color.startswith(('P', 'B', 'T'))]
['Red', 'Orange', 'Yellow', 'Green', 'Indigo', 'Violet']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Filter out all words that begin with P, B, or T. - by perfringo - May-06-2019, 06:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to write a code for term that can't begin or end with a stopword desul 3 4,610 Mar-18-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020