|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是我的代码:
- h = '''需要筛选的字符串'''
- a = 0
- b = []
- if h[0:3].isupper() and h[3].islower() and h[4:7].isupper() and not(h[7].isupper):
- b.append(h[3])
- while a+8 < len(h):
- if not(h[a].isupper()):
- if h[a+1].isupper():
- if h[a+2].isupper():
- if h[a+3].isupper():
- if h[a+4].islower():
- if h[a+5].isupper():
- if h[a+6].isupper():
- if h[a+7].isupper():
- if not(h[a+8].isupper()):
- b.append(h[a+4])
- a += 1
- print(b)
复制代码
我自己分析了一下和小甲鱼答案的思路,发现:
优点是在 for i in range(length)中每个字符仅参与寻找一遍,我的答案相当于除了前后几个字符,大部分字符都需要参与8遍运算,比较耗费资源。另外我的遇到\n时会判断为非大写字母,如果字符串为:
'''UTGgUY\nY'''
那g是不会被找出的,但是答案里的g就会,因为答案里认为\n两边的字符算连着的。
请大神帮忙分析一下我的答案与小甲鱼答案的其它利弊,多谢。
本帖最后由 jackz007 于 2020-11-19 17:16 编辑
- h = '''需要筛选的字符串'''
- s = 'a' + '' . join(h . split('\n')) + 'a' # 消灭 h 中的换行符 '\n',并在字符串首尾添加字符 'a' 以便判断规则同时适用于字符串内部和首尾
- p = ''
- for k in range(4 , len(s) - 4):
- if s[k - 4] . islower() and s[k - 3 : k] . isupper() and s[k] . islower() and s[k + 1 : k + 4] . isupper() and s[k + 4] . islower():
- p += s[k]
- print(p)
复制代码
|
|