【发布时间】:2011-06-10 11:31:47
【问题描述】:
string='a'
p=0
while (p <len(string)) & (string[p]!='c') :
p +=1
print ('the end but the process already died ')
while (p <1) & (string[p]!='c') :
IndexError: string index out of range
我想测试一个直到字符串结尾的条件(例如字符串长度=1)
为什么这两个部分的和执行的是条件已经是假的!
只要p < len(string)。第二部分甚至不需要执行。
如果它确实有很多性能可能会丢失
【问题讨论】:
-
你为什么使用按位
&运算符? -
这不是一些重新发明轮子的代码。
p = string.find(c)怎么样(如果不存在则返回 -1 而不是len(string)-1- 所以它更好,如 in,而不是模棱两可)?
标签: python comparison python-3.x boolean