【发布时间】:2017-01-02 19:10:33
【问题描述】:
我有一串乱七八糟的字母,有 1.2k 行长。 我正在尝试找到一个两边正好有三个大写字母的小写字母。
这是我目前所拥有的
def scramble(sentence):
try:
for i,v in enumerate(sentence):
if v.islower():
if sentence[i-4].islower() and sentence[i+4].islower():
....
....
except IndexError:
print() #Trying to deal with the problem of reaching the end of the list
#This section is checking if
the fourth letters before
and after i are lowercase to ensure the central lower case letter has
exactly three upper case letters around it
但现在我坚持下一步。我想要实现的是在(-3,4) 范围内创建一个for-loop,并检查这些字母中的每一个是否都是大写的。如果事实上小写字母的两边都有三个大写字母,那么打印出来。
例如
for j in range(-3,4):
if j != 0:
#Some code to check if the letters in this range are uppercase
#if j != 0 is there because we already know it is lowercase
#because of the previous if v.islower(): statement.
如果这没有意义,如果代码按预期工作,这将是一个示例输出
scramble("abcdEFGhIJKlmnop")
输出
EFGhIJK
一个小写字母,两边三个大写字母。
【问题讨论】:
-
为什么不使用正则表达式?
-
哇,人们还在玩 Python 挑战赛?很酷的豆子,很抱歉论坛已经被垃圾邮件机器人占领了。
-
我刚找到它,所以我想我会去@MartijnPieters 是的,不幸的是,论坛不好
-
在这种情况下不要使用循环,而是使用
regex。它会更有效率。类似问题的解决方案可在:Find a lowercase letter surronded by three uppercase letters
标签: python