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
#1
I have a set being passed to a function. Each element has a word in it with a mixture of upper and lower case characters. I need to find the one with the least uppercase letters and return it. I'm at a loss, but I'll show you what I've done so far. All the print statements are so I can see what I have done. 
count = str(group)
    print(count)
    count = sum(1 for character in group if character.islower())
    countString = ','.join(str(string) for string in group)
    print(countString)
    countString = countString.split()
    print(countString)
    countString = countString.strip().split(",")
    print(countString)
    print(count)
    
    #word.split(",")
    #print(word)
Any ideas?
Reply
#2
Why you count the lower case characters when you need to know how many upper case characters are in each word?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Make smaller problems:

  1. create a function that counts the number of upper case characters in a single word
  2. create a second function that
    1. sets some length variable some absurdly high value,
    2. iterates your words,
    3. if the count returned by the first function is smaller than the current value of length, updates length with that value and stores the corresponding word.
    4. Finally return the last value of length and the associated word.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
hi, try this to resolve your problem and change in indentation of function fun as required:
def fun(set_value):
   dic={}
   for i in set_value:
       s=0
       for j in i:
         if j.isupper():
               s+=1
       dic[i]=s
   return [j for j in dic if dic[j]==min([dic[i] for i in dic])]

set_value =set(['qweRW','SdfA','tfgAA','llQl','llPl','llPl'])
fun(set_value)
I have tried and its working...........
Reply
#5
Awesome, this is what I did.
fewestLetters = ''
    removeLetters = 26
    for i in group:
        iValue = len({x for x in i if x in ascii_uppercase})
        if iValue <= removeLetters:
            removeLetters = iValue
            fewestLetters = i
    return fewestLetters
Reply
#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
#7
Thanks, little late, but that's way better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding Row Number for Items in 2D array fafzal 2 2,430 Jan-10-2019, 06:11 AM
Last Post: fafzal
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 4,717 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