鱼C论坛

 找回密码
 立即注册
查看: 1245|回复: 4

[已解决]20讲动动手第二题

[复制链接]
发表于 2021-12-11 23:01:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. list1 = []

  2. count1 = 0#统计小写字母前三个大写字母个数
  3. count2 = 0#统计小写字母前小写字母个数


  4. with open('string2.txt') as f:
  5.     str1 = f.read()
  6.    
  7.     length = len(str1)

  8.     for i in range(length):
  9.         if str1[i].isupper():#大写
  10.             if count2:
  11.                 count1 += 1
  12.                 count2 = 0
  13.             else:
  14.                 count1 += 1
  15.             if count1 >= 3:
  16.                 count1 = 3
  17.         elif str1[i] == '\n':
  18.             continue
  19.         else:#a是小写
  20.             if count1 == 3:
  21.                 if count2:
  22.                     count1 = 0
  23.                     count2 = 0
  24.                 else:
  25.                     if i < length - 4:
  26.                         if str1[i+1].isupper() and str1[i+2].isupper() and str1[i+3].isupper() and str1[i+4].islower():
  27.                         #判断后面的四个字母是否为大写(三个大写一个小写)
  28.                             list1.append(str1[i])
  29.                             count1 = 0
  30.                             count2 = 1
  31.                         else:
  32.                             count1 = 0
  33.                             count2 = 1
  34.                     else:
  35.                         break
  36.             else:
  37.                 count1 = 0
  38.                 count2 = 1
  39. print(*list1)

  40. #此方法无法保证小写字母前三个字母是连续的大写字母
复制代码


我这个方法能实现么,怎么判断小写字母前的大写字母是连续的呢,求教。
最佳答案
2021-12-11 23:42:16
  1. with open('string2.txt') as f:
  2.     str1 . read()
  3. s , p = 'a' + '' . join(str1 . split('\n')) + 'a' , ''
  4. for k in range(4 , len(s) - 4):
  5.     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]
  6. print(p)
复制代码
屏幕截图 2021-12-11 225803.png
屏幕截图 2021-12-11 225925.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-11 23:42:16 | 显示全部楼层    本楼为最佳答案   
  1. with open('string2.txt') as f:
  2.     str1 . read()
  3. s , p = 'a' + '' . join(str1 . split('\n')) + 'a' , ''
  4. for k in range(4 , len(s) - 4):
  5.     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]
  6. print(p)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-11 23:42:58 From FishC Mobile | 显示全部楼层
  1. f = lambda s: s[:3].isupper() and s[3:-3].islower() and s[-3:].isupper()
  2. print(f("ABCabcdfABC"))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-12 16:54:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-12 16:56:00 | 显示全部楼层

lambda表达式返回值是True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表