1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        判斷字符串是否包含子串,居然有七種方法?

        共 2029字,需瀏覽 5分鐘

         ·

        2021-01-13 02:14




        系列導(dǎo)讀


        Python 炫技操作(01):條件語句的七種寫法

        Python 炫技操作(02):合并字典的七種方法

        Python 炫技操作(03):連接列表的八種方法

        Python 炫技操作(04):海象運(yùn)算符的三種方法

        Python 炫技操作(05):花式導(dǎo)包的八種方法


        1、使用 in 和 not in

        innot in 在 Python 中是很常用的關(guān)鍵字,我們將它們歸類為 成員運(yùn)算符。

        使用這兩個(gè)成員運(yùn)算符,可以很讓我們很直觀清晰的判斷一個(gè)對(duì)象是否在另一個(gè)對(duì)象中,示例如下:

        >>>?"llo"?in?"hello,?python"
        True
        >>>
        >>>?"lol"?in?"hello,?python"
        False

        2、使用 find 方法

        使用 字符串 對(duì)象的 find 方法,如果有找到子串,就可以返回指定子串在字符串中的出現(xiàn)位置,如果沒有找到,就返回 -1

        >>>?"hello,?python".find("llo")?!=?-1
        True
        >>>?"hello,?python".find("lol")?!=?-1
        False
        >>

        3、使用 index 方法

        字符串對(duì)象有一個(gè) index 方法,可以返回指定子串在該字符串中第一次出現(xiàn)的索引,如果沒有找到會(huì)拋出異常,因此使用時(shí)需要注意捕獲。

        def?is_in(full_str,?sub_str):
        ????try:
        ????????full_str.index(sub_str)
        ????????return?True
        ????except?ValueError:
        ????????return?False

        print(is_in("hello,?python",?"llo"))??#?True
        print(is_in("hello,?python",?"lol"))??#?False

        4、使用 count 方法

        利用和 index 這種曲線救國(guó)的思路,同樣我們可以使用 count 的方法來判斷。

        只要判斷結(jié)果大于 0 就說明子串存在于字符串中。

        def?is_in(full_str,?sub_str):
        ????return?full_str.count(sub_str)?>?0

        print(is_in("hello,?python",?"llo"))??#?True
        print(is_in("hello,?python",?"lol"))??#?False

        5、通過魔法方法

        在第一種方法中,我們使用 in 和 not in 判斷一個(gè)子串是否存在于另一個(gè)字符中,實(shí)際上當(dāng)你使用 in 和 not in 時(shí),Python 解釋器會(huì)先去檢查該對(duì)象是否有 __contains__ 魔法方法。

        若有就執(zhí)行它,若沒有,Python 就自動(dòng)會(huì)迭代整個(gè)序列,只要找到了需要的一項(xiàng)就返回 True 。

        示例如下;

        >>>?"hello,?python".__contains__("llo")
        True
        >>>
        >>>?"hello,?python".__contains__("lol")
        False
        >>>

        這個(gè)用法與使用 in 和 not in 沒有區(qū)別,但不排除有人會(huì)特意寫成這樣來增加代碼的理解難度。

        6、借助 operator

        operator模塊是python中內(nèi)置的操作符函數(shù)接口,它定義了一些算術(shù)和比較內(nèi)置操作的函數(shù)。operator模塊是用c實(shí)現(xiàn)的,所以執(zhí)行速度比 python 代碼快。

        在 operator 中有一個(gè)方法 contains 可以很方便地判斷子串是否在字符串中。

        >>>?import?operator
        >>>
        >>>?operator.contains("hello,?python",?"llo")
        True
        >>>?operator.contains("hello,?python",?"lol")
        False
        >>>?

        7、使用正則匹配

        說到查找功能,那正則絕對(duì)可以說是專業(yè)的工具,多復(fù)雜的查找規(guī)則,都能滿足你。

        對(duì)于判斷字符串是否存在于另一個(gè)字符串中的這個(gè)需求,使用正則簡(jiǎn)直就是大材小用。

        import?re

        def?is_in(full_str,?sub_str):
        ????if?re.findall(sub_str,?full_str):
        ????????return?True
        ????else:
        ????????return?False

        print(is_in("hello,?python",?"llo"))??#?True
        print(is_in("hello,?python",?"lol"))??#?False



        長(zhǎng)按下圖? ?? ?關(guān)注博主

        (按左邊關(guān)注?Python, 按右邊關(guān)注?Goalng


        瀏覽 53
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            天堂国产在线视频 | 欧美操逼电影 | 国产很黄很裸露的电视剧 | 肏屄视频网站 | 99精品店视频 | 18禁超污网站 | 欧美伦理一区二区三区 | 欧美大片在线观看 | 国产freexxxx性麻豆古装 | 看操B |