|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- list1 = []
- count1 = 0#统计小写字母前三个大写字母个数
- count2 = 0#统计小写字母前小写字母个数
- with open('string2.txt') as f:
- str1 = f.read()
-
- length = len(str1)
- for i in range(length):
- if str1[i].isupper():#大写
- if count2:
- count1 += 1
- count2 = 0
- else:
- count1 += 1
- if count1 >= 3:
- count1 = 3
- elif str1[i] == '\n':
- continue
- else:#a是小写
- if count1 == 3:
- if count2:
- count1 = 0
- count2 = 0
- else:
- if i < length - 4:
- if str1[i+1].isupper() and str1[i+2].isupper() and str1[i+3].isupper() and str1[i+4].islower():
- #判断后面的四个字母是否为大写(三个大写一个小写)
- list1.append(str1[i])
- count1 = 0
- count2 = 1
- else:
- count1 = 0
- count2 = 1
- else:
- break
- else:
- count1 = 0
- count2 = 1
- print(*list1)
- #此方法无法保证小写字母前三个字母是连续的大写字母
复制代码
我这个方法能实现么,怎么判断小写字母前的大写字母是连续的呢,求教。
- with open('string2.txt') as f:
- str1 . read()
- s , p = 'a' + '' . join(str1 . split('\n')) + 'a' , ''
- 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)
复制代码
|
-
-
|