Python 索引器错误:字符串索引超出范围:再次
我有下一个问题。该代码非常适合捕获标签,但是。。。我怎样才能在第二个“while”中输入车道已完工 如果我放在这条车道上:Python 索引器错误:字符串索引超出范围:再次,python,Python,我有下一个问题。该代码非常适合捕获标签,但是。。。我怎样才能在第二个“while”中输入车道已完工 如果我放在这条车道上: hash = '#hhhh #asdasd' 代码无法编译。我怎样才能改变时间?先谢谢你。 例外情况: while hash[indexA + 1] not in {' ', '#'}: IndexError: string index out of range 代码如下: hash = '#hhhh #asdasd ' indexA = 0 copy = '' in
hash = '#hhhh #asdasd'
代码无法编译。我怎样才能改变时间?先谢谢你。
例外情况:
while hash[indexA + 1] not in {' ', '#'}:
IndexError: string index out of range
代码如下:
hash = '#hhhh #asdasd '
indexA = 0
copy = ''
indexB = indexA
while indexA < len(hash):
indexB = indexA
copy = ''
if hash[indexA] == '#' and indexA + 1 < len(hash):
while hash[indexA + 1] not in {' ', '#'}:
indexA += 1
copy = hash[indexB:indexA + 1]
if len(copy) > 1:
print('newHashtag: ' + copy)
if hash[indexA + 1] == ' ':
indexA += 1
else:
indexA += 1
hash='#hhh#asdasd'
指数=0
复制=“”
indexB=indexA
而indexA1:
打印('newHashtag:'+复制)
如果哈希[indexA+1]='':
indexA+=1
其他:
indexA+=1
索引有问题,因为您没有检查哈希长度。这应该起作用:
hash = '#hhhh #asdasd'
indexA = 0
copy = ''
indexB = indexA
while indexA < len(hash):
indexB = indexA
copy = ''
if hash[indexA] == '#' and indexA + 1 < len(hash):
while indexA + 1 < len(hash) and hash[indexA + 1] not in {' ', '#'}:
indexA += 1
copy = hash[indexB:indexA + 1]
if len(copy) > 1:
print('newHashtag: ' + copy)
if indexA + 1 < len(hash) and hash[indexA + 1] == ' ':
indexA += 1
else:
indexA += 1
hash='#hhh#asdasd'
指数=0
复制=“”
indexB=indexA
而indexA1:
打印('newHashtag:'+复制)
如果indexA+1
索引有问题,因为您没有检查哈希长度。这应该起作用:
hash = '#hhhh #asdasd'
indexA = 0
copy = ''
indexB = indexA
while indexA < len(hash):
indexB = indexA
copy = ''
if hash[indexA] == '#' and indexA + 1 < len(hash):
while indexA + 1 < len(hash) and hash[indexA + 1] not in {' ', '#'}:
indexA += 1
copy = hash[indexB:indexA + 1]
if len(copy) > 1:
print('newHashtag: ' + copy)
if indexA + 1 < len(hash) and hash[indexA + 1] == ' ':
indexA += 1
else:
indexA += 1
hash='#hhh#asdasd'
指数=0
复制=“”
indexB=indexA
而indexA1:
打印('newHashtag:'+复制)
如果indexA+1
如果您想在字符串中查找标记,您可以更轻松地使用Python方式:
some_string = "#tag1 #tag2 something else #tag3"
tags = [tag.strip("#") for tag in some_string.split() if tag.startswith("#")]
>>> print tags
['tag1', 'tag2', 'tag3']
如果标签没有空格,您可以这样写:
some_string = "#tag1 #tag2 something else #tag3 #moretags#andmore"
tags = []
for tag in some_string.split():
if '#' in tag:
multi_tags = tag.split('#')
tags.extend([t for t in multi_tags if t])
>>> tags
['tag1', 'tag2', 'tag3', 'moretags', 'andmore']
如果您想在字符串中查找标记,可以更轻松地使用Python方式:
some_string = "#tag1 #tag2 something else #tag3"
tags = [tag.strip("#") for tag in some_string.split() if tag.startswith("#")]
>>> print tags
['tag1', 'tag2', 'tag3']
如果标签没有空格,您可以这样写:
some_string = "#tag1 #tag2 something else #tag3 #moretags#andmore"
tags = []
for tag in some_string.split():
if '#' in tag:
multi_tags = tag.split('#')
tags.extend([t for t in multi_tags if t])
>>> tags
['tag1', 'tag2', 'tag3', 'moretags', 'andmore']
而indexA
此代码在我的Python解释器中工作。顺便说一句,hash
是Python中的一个内置函数,覆盖它不是一个好主意。如果我理解你在做什么,你应该真正使用正则表达式。如果字符串总是规则的,甚至可以使用split
。而indexA
此代码在我的Python解释器中工作。顺便说一句,hash
是Python中的一个内置函数,覆盖它不是一个好主意。如果我理解你在做什么,你应该真正使用正则表达式。如果字符串总是规则的,您甚至可以使用split
。根据操作码,标记之间可能没有空格,因此您的解决方案可能不起作用。@Danicroquebelmar,在使用Python多年的经验后,您将像我一样编程:)但请尝试使用Python功能解决您的任务,但不要使用Python编写类似Basic的代码:)根据OP代码,标记之间可能没有空格,因此您的解决方案可能不起作用。@Danicroquebelmar,在使用Python多年的经验后,您将像我一样编程:)但请尝试使用Python功能解决您的任务,但不要使用Python编写类似Basic的代码:)