Python Forum
Finding Number of Lowercase letters in a Set
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding Number of Lowercase letters in a Set
#6
Here is a simple one-liner solution, using RE and min function

import re
shortest = min(words, key=lambda w: len(re.findall('[A-Z]', w)))
As a lambda function you may also use something similar to the way you counted lower case - with a little twist
sum(c.isupper() for c in w)
which may be or may not be more efficient  Huh .

Here I use (abuse  Tongue ?!) the fact that Python allows implicit convert of False and True to 0 and 1  - which makes writing conditional increments very easy.

PS While I applaud using meaningful names for variables (one-letter names must be made illegal - unless in one-liner code snippets Angry ), I would suggest to use Pythonic naming conventions - see PEP-8
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
RE: Finding Number of Lowercase letters in a Set - by volcano63 - Apr-15-2017, 09:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding Row Number for Items in 2D array fafzal 2 3,200 Jan-10-2019, 06:11 AM
Last Post: fafzal
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 5,556 Jan-01-2018, 05:03 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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