zoukankan      html  css  js  c++  java
  • python find 返回元素的索引

    As it turns out, there is a string method named find that is remarkably similar to the function we wrote:

    >>> word = 'banana'
    >>> index = word.find('a')
    >>> index
    1

    In this example, we invoke find on word and pass the letter we are looking for as a param- eter.

    Actually, the find method is more general than our function; it can find substrings, not just characters:

    >>> word.find('na')
    2

    By default, find starts at the beginning of the string, but it can take a second argument, the index where it should start:

    >>> word.find('na', 3)
    4

    This is an example of an optional argument; find can also take a third argument, the index where it should stop:

    >>> name = 'bob'
    >>> name.find('b', 1, 2)
    -1

    This search fails because b does not appear in the index range from 1 to 2, not including 2. Searching up to, but not including, the second index makes find consistent with the slice operator. 

  • 相关阅读:
    BZOJ 2588
    BZOJ 3524
    BZOJ 3932
    Bzoj1013--Jsoi2008球形空间产生器
    Codevs1743--反转卡片
    Bzoj1208--Hnoi2004宠物收养所
    Bzoj1112--Poi2008砖块Klo
    后缀自动机学习笔记
    Bzoj1588--Hnoi2002营业额统计
    Bzoj1056--Haoi2008排名系统
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10136425.html
Copyright © 2011-2022 走看看