[英]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.