經(jīng)常寫(xiě) shell 腳本知道,字符串判斷可以用 =,!= 數(shù)字的判斷是 -eq,-ne 等,但是 Python 確不是這樣子的。
所以作為慢慢要轉(zhuǎn)換到用 Python 寫(xiě)腳本,這些基本的東西必須要掌握到骨子里!
在 Python 中比較字符串最好是使用簡(jiǎn)單邏輯操作符。
例如,確定一個(gè)字符串是否和另外一個(gè)字符串匹配。正確的,你可以使用 is equal 或 == 操作符。你也可以使用例如 >= 或 < 來(lái)確定幾個(gè)字符串的排列順序。
從官方文檔上看
The operators ``is`` and ``is not`` test for object identity: ``x is y`` is true if and only if *x* and *y* are the same object. ``x is not y`` yields the inverse truth value. cmp(...) cmp(x, y) -> integer Return negative if xy.
也就是說(shuō) is 用來(lái)判斷是否是同一個(gè)對(duì)象,is 是種很特殊的語(yǔ)法,你在其它的語(yǔ)言應(yīng)該不會(huì)見(jiàn)到這樣的用法。
python is 主要是判斷 2 個(gè)變量是否引用的是同一個(gè)對(duì)象,如果是的話,則返回 true,否則返回 false。
判斷數(shù)字相等不要用 is 操作符
>>> a = 256 >>> b = 256 >>> id(a) 9987148 >>> id(b) 9987148 >>> a = 257 >>> b = 257 >>> id(a) 11662816 >>> id(b) 11662828
為什么兩次 is 返回的是不同結(jié)果?不是應(yīng)該都是 true 嗎?
因?yàn)?string pooling (或叫intern)。 is 相等代表兩個(gè)對(duì)象的 id 相同(從底層來(lái)看的話,可以看作引用同一塊內(nèi)存區(qū)域)。 至于為什么 “ABC” 被 intern 了而 “a bc” 沒(méi)有,這是 Python 解析器實(shí)現(xiàn)決定的,可能會(huì)變。
== 用來(lái)判斷兩個(gè)對(duì)象的值是否相等(跟 Java 不同,Java 中 == 用來(lái)判斷是否是同一個(gè)對(duì)象)。
今天我用 == 來(lái)判斷兩個(gè) IP 地址 字符串是否相同。
#!/usr/bin/python strtmp = '192.169.1.161' file_object = open(r'public_ip.txt') try: all_the_text = file_object.readlines() firstline = all_the_text[0].rstrip() finally: file_object.close() #print firstline #if strtmp == firstline: s = (strtmp is firstline) print s if (strtmp is firstline): print 'yes' else: print 'no'
來(lái)個(gè)簡(jiǎn)單點(diǎn)的例子:
#-*-conding:utf-8-*- i='xinwen'; m=input(); if i==m: print('yes'); else: print('no'); input();
在 if 判斷語(yǔ)句中非常有用吶!
#!/usr/bin/python # Filename: if.py number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block ... else: print 'No, it is a little lower than that' # you must have guess > number to reach here print 'Done' # This last statement is always executed, after the if statement is executed
cmp() 函數(shù)則是相當(dāng)于 <,==,> 但是在 Python3 中,cmp() 函數(shù)被移除了,所以我以后還是避免少用這個(gè)函數(shù)。
>>> x='a' >>> x+'b' is 'ab' False >>> x+'b' == 'ab' True >>> cmp(x+'b','ab') 0 >>> id(x+'b') 32468384L >>> id('ab') 46933696L >>>
注意:
>>> a='abc' >>> b='abc' >>> a is b True >>> id(a) == id(b) True >>>
可以看出內(nèi)容相同的字符串實(shí)際上是同一個(gè)對(duì)象(Java 中直接賦值的字符串也可用 == 來(lái)判斷,但是使用 new 實(shí)例化的對(duì)象則需要使用equals(String s) 來(lái)判斷)。
以上幾個(gè)例子大家應(yīng)該可以明白在python中字符串比較使用is、==和cmp()的方法了
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
