简体   繁体   English

Python-从字符串列表中删除小写或大写字母?

[英]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. -仅计算大写字母(didnt工作)-删除小写字母,然后执行min(list,key = len),但这也不起作用。

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 现在,这只是返回列表中最短的列表中的第一个字符串

Why not use only min with a lambda that counts the number of uppercases? 为什么不只使用带有大写数字的lambda的min

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

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. 哦,顺便说一句,如果您将其传递为空列表,则此操作会崩溃,因此如果使用它,请当心。

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 从字符串列表中删除小写或大写字母

(ie your title) is not going to be the best way to do it. (即您的标题)将不是最好的方法。 (This is an example of the "XY problem".) (这是“ XY问题”的示例。)

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. 您可以看到ranked[0][1]是最小的字符串,而ranked[-1][1]是最大的字符串。 You could directly use min() instead of sorted() to get the smallest-number string 您可以直接使用min()而不是sorted()来获取最小数目的字符串

>>> 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). 但我认为使用sorted可以为您提供更丰富的信息,使您可以检查联系(以时间和内存复杂性为代价)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 忽略大写/小写的字符串排序列表 - Sort list of strings ignoring upper/lower case 计算字符串中小写字母和大写字母个数的 Python 函数 - Python function that counts number of lower case and upper case letters in a string Python:如何匹配字符串列表中的特定单词,无论它是由大写还是小写字母组成 - Python: How to match a specific word in a list of strings, no matter it consists of upper or lower letters 用 Python 阅读主题并计算大小写字母 - Reading subject in Python and counting upper and lower case letters 在python中,如何使函数接受大小写字母相同? - In python, how to make a function accept lower and upper case letters as the same? 编写一个接受字符串并计算大写字母和小写字母个数的Python函数 - Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters 我如何在python3中将大小写从大写翻转为小写以及从小写翻转为大写 - How can i flip Case from upper to lower case and from lower to upper case in python3 写一个 Python function 接受一个字符串,计算大写字母和小写字母的个数。 编程新手 - Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. new to programming 在仅大小写不同的字符串列表中查找重复项 - Find duplicates in a list of strings differing only in upper and lower case writing 如何在字符串中分隔大写和小写字母 - How to separate upper and lower case letters in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM