20讲动动手第二题
list1 = []count1 = 0#统计小写字母前三个大写字母个数
count2 = 0#统计小写字母前小写字母个数
with open('string2.txt') as f:
str1 = f.read()
length = len(str1)
for i in range(length):
if str1.isupper():#大写
if count2:
count1 += 1
count2 = 0
else:
count1 += 1
if count1 >= 3:
count1 = 3
elif str1 == '\n':
continue
else:#a是小写
if count1 == 3:
if count2:
count1 = 0
count2 = 0
else:
if i < length - 4:
if str1.isupper() and str1.isupper() and str1.isupper() and str1.islower():
#判断后面的四个字母是否为大写(三个大写一个小写)
list1.append(str1)
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 . islower() and s . isupper() and s . islower() and s . isupper() and s . islower() : p += s
print(p) f = lambda s: s[:3].isupper() and s.islower() and s[-3:].isupper()
print(f("ABCabcdfABC")) jackz007 发表于 2021-12-11 23:42
感谢指导{:5_110:} 傻眼貓咪 发表于 2021-12-11 23:42
lambda表达式返回值是True
页:
[1]