鱼C论坛

 找回密码
 立即注册
查看: 2947|回复: 3

[已解决]零基础20讲

[复制链接]
发表于 2018-1-15 17:46:33 | 显示全部楼层 |阅读模式

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

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

x
word = ''' string2'''
lenth = len(word)
list1= []
for each in range(lenth):
    i=1
    if word[each] == '\n':
        continue
    for i in range(3):
        if word[each-i].isupper and word[each+i].isupper and word[each].islower:
            list1.append(each)
print (list1)
            
这样总是报错,说无法string  index out of range,怎么回事呢
最佳答案
2018-1-21 18:48:17
本帖最后由 8306最硬 于 2018-1-21 18:49 编辑
ZCYDHHX 发表于 2018-1-19 16:26
那怎么修改呢?


可以用切片操作,按你的思路改了下
  1. word = ''' string2'''
  2. word = ''.join(word.split())
  3. lenth = len(word)
  4. list1= []
  5. for each in range(lenth):
  6.     if word[each-3:each].isupper()\
  7.     and word[each+1:each+4].isupper()\
  8.     and word[each].islower()\
  9.     and word[each+4].islower()\
  10.     and word[each-4].islower():
  11.             list1.append(word[each])
  12. print (''.join(list1))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-15 17:49:32 | 显示全部楼层
中间的word[each+i],其中each+i超过了字符串的索引范围
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-19 16:26:14 | 显示全部楼层
8306最硬 发表于 2018-1-15 17:49
中间的word[each+i],其中each+i超过了字符串的索引范围

那怎么修改呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-21 18:48:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 8306最硬 于 2018-1-21 18:49 编辑
ZCYDHHX 发表于 2018-1-19 16:26
那怎么修改呢?


可以用切片操作,按你的思路改了下
  1. word = ''' string2'''
  2. word = ''.join(word.split())
  3. lenth = len(word)
  4. list1= []
  5. for each in range(lenth):
  6.     if word[each-3:each].isupper()\
  7.     and word[each+1:each+4].isupper()\
  8.     and word[each].islower()\
  9.     and word[each+4].islower()\
  10.     and word[each-4].islower():
  11.             list1.append(word[each])
  12. print (''.join(list1))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 15:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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