user2951723
user2951723

Reputation: 83

Python - Removing lower or upper case letters from a list of strings?

So I'm a noob programmer...dont hate my code. But I'm struggling a bit here. I'm trying to take a list of strings, and find the string that has the lowest amount of upper case letters.

I've tried a few different things. -Just counting the upper case(didnt work) -Removing lower case and then doing min(list, key=len) but that didnt work either.

I'm stuck...heres what I got as of now.

 test_set = {'MOo', 'QHue', 'ReP', 'XiIV', 'oEe'}

 def fewest_unsolved(group):
     #shortest = min(group, key = len)
     #return shortest

     for word in group:

         for i in word:
             if i == i.lower():
                 word.strip(i)
             shortest = min(group, key = len)
             return shortest   

fewest_unsolved(test_set)

Now this just returns the first string in the list that is the shortest of the list

Upvotes: 0

Views: 3162

Answers (3)

jez
jez

Reputation: 15349

If your goal is truly well summarized by

I'm trying to take a list of strings, and find the string that has the lowest amount of upper case letters.

then probably

Removing lower or upper case letters from a list of strings

(i.e. your title) is not going to be the best way to do it. (This is an example of the "X-Y problem".)

This will do the former (but not the latter):

>>> test_set = {'MOo', 'QHue', 'ReP', 'XiIV', 'oEe'}
>>> ranked = sorted([sum('A'<=c<='Z' for c in s), s] for s in test_set) 
>>> print(ranked)
[[1, 'oEe'], [2, 'MOo'], [2, 'QHue'], [2, 'ReP'], [3, 'XiIV']]

You can see that ranked[0][1] is the string with the smallest, and ranked[-1][1] the string with the largest, number of capitals. You could directly use min() instead of sorted() to get the smallest-number string

>>> capcount, string = min( [sum('A'<=c<='Z' for c in s), s] for s in test_set )

but I figured using sorted would give you richer information that would allow you to check for ties (at the expense of time- and memory-complexity).

Upvotes: 0

Pedro Castilho
Pedro Castilho

Reputation: 10522

You can do this using a list comprehension:

def lowercase_count(word):
  lowercase = [c for c in word if c.islower()]
  return len(lowercase)

Knowing the count of lowercase words,you can then do the following:

def fewest_unsolved(group):
  least = lowercase_count(group[0])
  current = group[0]
  for word in group[1:]:
    count = lowercase_count(word)
    if count < least:
      least = count
      current = word
  return current

This is a bit more verbose than some other solutions, but I think it might be a bit more readable :)

Oh, by the way, this crashes if you pass it an empty list, so be careful if you use it.

Upvotes: 1

Uriel
Uriel

Reputation: 16184

Why not use only min with a lambda that counts the number of uppercases?

>>> data = {'MOo', 'QHue', 'ReP', 'XiIV', 'oEe'}
>>> min(data, key = lambda x: sum('A' <= c <= 'Z' for c in x))
'oEe'

Upvotes: 1

Related Questions