Skip to content

Find the position · ⁨找到位置⁩

English

Find the position

Search a list and report where something is, counting from 0.

Loop over the positions with range(len(items)), so you have the index i as well as the item items[i]. return leaves the function straight away, which is exactly what you want at the first match. Return -1 only after the loop has checked every item.

中文

找到位置

搜索列表,报告某个元素在哪里,位置从 0 开始计数。

用 range(len(items)) 遍历位置,这样你既有下标 i,又有元素 items[i]。return 会立刻离开函数,这正是找到第一个匹配时需要的。只有在循环检查完所有元素之后,才返回 -1。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩