python2.7.5中的find()函数 [英] find() function in python2.7.5

查看:31
本文介绍了python2.7.5中的find()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

find('asdf','')'asdf' 中找到一个空字符串,因此它返回 0.类似地,find('asdf','',3) 开始搜索索引位置3 处的字符串,因此返回3.由于最后一个索引是 3find('asdf','',4) 应该返回 -1 但它返回 4 并且只有当起始索引大于或等于 (last_index)+2 时才开始返回 -1.为什么会这样?

find('asdf','') finds an empty string in 'asdf' hence it returns 0. Similarly, find('asdf','',3) starts to search for the string at index position 3 and hence return 3. Since the last index is 3, find('asdf','',4) should return -1 but it returns 4 and starts to return -1 only if the starting index is more than or equal to (last_index)+2. Why is this so?

推荐答案

这是它的工作原理:

0 a 1 s 2 d 3 f 4

当您使用 'asdf'.find(sub) 时,它会搜索这五个位置,标记为 0、1、2、3 和 4.这五个位置.那五个.不多也不少.它返回第一个 'asdf'[pos:pos+len(sub)] == sub.如果将 start 参数包含在 find 中,则它从该位置开始.那个位置.一点也不少,一点也不多.如果您给出的起始位置大于位置列表中的最大数字,则返回 -1.

When you use 'asdf'.find(sub), it searches those five positions, labeled 0, 1, 2, 3, and 4. Those five. Those five. No more and no less. It returns the first one where 'asdf'[pos:pos+len(sub)] == sub. If you include the start argument to find, it starts at that position. That position. Not one less, not one more. If you give a start position greater than the greatest number in the list of positions, it returns -1.

换句话说,答案是我已经在评论中重复的,引用了另一个问题的答案:

In other words, the answer is what I already repeated in a comment, quoting another question answer:

最后一个位置[for find] 字符串的最后一个字符之后

The last position [for find] is after the last character of the string

您的基本误解似乎与字符串中的位置"概念有关.find 不会返回您希望作为单个单元访问的位置.即使它返回 4,也不意味着空字符串在"位置 4.find 返回 slice starts.您应该从给定位置开始切片字符串.

it seems your fundamental misunderstanding relates to the notion of "positions" in a string. find is not returning positions which you are expected to access as individual units. Even if it returns 4, that doesn't mean the empty string is "at" position 4. find returns slice starts. You are supposed to slice the string starting at the given position.

所以当你执行 'asdf'.find('', 4) 时,它从位置 4 开始.它在那里找到空字符串,因为 'asdf'[4:4+len('')]==''.它返回 4.这就是它的工作原理.

So when you do 'asdf'.find('', 4), it starts at position 4. It finds the empty string there because 'asdf'[4:4+len('')]==''. It returns 4. That is how it works.

str.find 并不打算在有效索引到实际字符串之间建立一对一的映射.是的,您可以进行大量其他索引,例如 'asdf'[100:300].这与 find 无关.你从 find 知道的是 'asdf'[pos:pos+len(sub)] == sub.您不知道每个可能返回 '' 的索引都会被 find 返回,也不能保证 find 返回的数字是字符串的有效索引如果您搜索的是空字符串.

It is not intended that str.find has a one-to-one mapping between valid indexes into the actual string. Yes, you can do tons of other indexing like 'asdf'[100:300]. That is not relevant for find. What you know from find is that 'asdf'[pos:pos+len(sub)] == sub. You do not know that every possible index that would return '' will be returned by find, nor are you guaranteed that the number returned by find is a valid index into the string if you searched for an empty string.

如果您对该功能的某些使用有实际疑问,请继续将其作为一个单独的问题提出.但是您似乎已经知道 find 是如何工作的,所以不清楚您希望从这个问题中获得什么.

If you have an actual question about some use of this functionality, then go ahead and ask that as a separate question. But you seem to already know how find works, so it's not clear what you're hoping to gain from this question.

这篇关于python2.7.5中的find()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆