[英]Python: index of first non-matching character?
Python 2.7: I'm wondering if there's an inverted version of the index operation for a list. Python 2.7:我想知道列表的索引操作是否存在反向版本。 I can't use
index
because the value that will not match is unknown. 我无法使用
index
因为不匹配的值是未知的。
Here's an example: 这是一个例子:
myString = "aaaaaxcbaa"
pos = myString.not_index('a')
# pos should be 5
myString = "bbzcbaa"
pos = myString.not_index('b')
# pos should be 2
myString = "xxxxx"
pos = myString.not_index('x')
# this can throw an error similar to "index"
Does such a thing exist? 这样的事情存在吗?
Right now I'm simply iterating the list item-by-item but it feels like this should be a bit simpler. 现在,我只是逐项迭代列表,但是感觉应该更简单一些。
No. 没有。
>>> next(i for (i, e) in enumerate("aaaaaxcbaa") if e != "a")
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.