3 回答

TA貢獻1818條經(jīng)驗 獲得超3個贊
strg = "abcdefghi"
print(strg.endswith("ghi")) # True
print(strg.endswith("def", 1, 6)) # True
最后一個語句與
print(strg[1:6].endswith("def") # True
除了它不會創(chuàng)建一個新的字符串(切片會這樣做)。strg[1:6] = "bcdef"

TA貢獻1865條經(jīng)驗 獲得超7個贊
我認為這和他們提到的參數(shù),在那里可以提高代碼的可讀性,并消除你通過切片獲得的復雜性。str.startswith()str.endswith()startend
text = "afoobarstring"
part_to_check = "bar"
position = 4
# approach 1 (startswith)
text.startswith(part_to_check, position)
# approach 2 (slicing)
text[position:position + len(part_to_check)] == part_to_check
# approach 3 (startswith and slicing)
text[position:].startswith(part_to_check)
雖然這三種方法做大致相同的事情,但方法1比其他方法更容易閱讀和理解(恕我直言)。在方法 2 中,您還必須計算字符串的結(jié)束位置,在方法 3 中,您還有字符串的副本(如方法 2 中所示)和與方法 1 中相同的函數(shù)調(diào)用。
我懷疑這也會導致更好的時間測量結(jié)果:
In [1]: import timeit
In [2]: timeit.timeit('text="afoobarstring"; part_to_check = "bar"; position = 4; text.startswith(part_to_check, position)', number=10000000)
Out[2]: 1.6531553190034174
In [3]: timeit.timeit('text="afoobarstring"; part_to_check = "bar"; position = 4; text[position:position + len(part_to_check)] == part_to_check', number=10000000)
Out[3]: 1.8180583719986316
In [4]: timeit.timeit('text="afoobarstring"; part_to_check = "bar"; position = 4; text[position:].startswith(part_to_check)', number=10000000)
Out[4]: 2.349334787999396
而且我認為方法1更干凈,也是在引擎蓋下:
In [1]: import dis
In [2]: def startswith():
...: text="afoobarstring"
...: part_to_check = "bar"
...: position = 4
...: return text.startswith(part_to_check, position)
...:
In [3]: def slicing():
...: text="afoobarstring"
...: part_to_check = "bar"
...: position = 4
...: return text[position:position + len(part_to_check)] == part_to_check
...:
In [4]: def slicing_and_startswith():
...: text="afoobarstring"
...: part_to_check = "bar"
...: position = 4
...: return text[position:].startswith(part_to_check)
...:
In [5]: dis.dis(startswith)
2 0 LOAD_CONST 1 ('afoobarstring')
3 STORE_FAST 0 (text)
3 6 LOAD_CONST 2 ('bar')
9 STORE_FAST 1 (part_to_check)
4 12 LOAD_CONST 3 (4)
15 STORE_FAST 2 (position)
5 18 LOAD_FAST 0 (text)
21 LOAD_ATTR 0 (startswith)
24 LOAD_FAST 1 (part_to_check)
27 LOAD_FAST 2 (position)
30 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
33 RETURN_VALUE
In [6]: dis.dis(slicing)
2 0 LOAD_CONST 1 ('afoobarstring')
3 STORE_FAST 0 (text)
3 6 LOAD_CONST 2 ('bar')
9 STORE_FAST 1 (part_to_check)
4 12 LOAD_CONST 3 (4)
15 STORE_FAST 2 (position)
5 18 LOAD_FAST 0 (text)
21 LOAD_FAST 2 (position)
24 LOAD_FAST 2 (position)
27 LOAD_GLOBAL 0 (len)
30 LOAD_FAST 1 (part_to_check)
33 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
36 BINARY_ADD
37 BUILD_SLICE 2
40 BINARY_SUBSCR
41 LOAD_FAST 1 (part_to_check)
44 COMPARE_OP 2 (==)
47 RETURN_VALUE
In [7]: dis.dis(slicing_and_startswith)
2 0 LOAD_CONST 1 ('afoobarstring')
3 STORE_FAST 0 (text)
3 6 LOAD_CONST 2 ('bar')
9 STORE_FAST 1 (part_to_check)
4 12 LOAD_CONST 3 (4)
15 STORE_FAST 2 (position)
5 18 LOAD_FAST 0 (text)
21 LOAD_FAST 2 (position)
24 LOAD_CONST 0 (None)
27 BUILD_SLICE 2
30 BINARY_SUBSCR
31 LOAD_ATTR 0 (startswith)
34 LOAD_FAST 1 (part_to_check)
37 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
40 RETURN_VALUE
我認為它與 -方法類似。這就是為什么我沒有單獨展示這個。endswith()

TA貢獻1842條經(jīng)驗 獲得超13個贊
startswith/endwith的目的是在某個字符串的開頭或結(jié)尾找到一些子字符串。
開始和結(jié)束參數(shù)的目的是“滑動”窗口,您可以在其中找到子字符串。用作參數(shù),因此可以應用 startswith/endwith 來查找任何特定位置的任何子字符串。
添加回答
舉報