python快速查詢字串中的某個字元

2021-10-09 03:03:24 字數 481 閱讀 7763

import re  

text =

"/user/lijian/desktop/workspace/project/v501/name/"

searchcontext =

"/"

numlist =

[m.start(

)for m in re.finditer(searchcontext, text)

]print numlist

比如我們要查詢text字串中的字元/出現的所有位置。

print出的內容是 [0, 5, 13, 21, 31, 39, 44, 49]

這樣所有的位置就存放到了乙個list中,比如我們想獲取最後的name資料夾,可以通過下邊的**實現:

print text[numlist[-2]+1:numlist[-1]]

其實這些小技巧在我們的工作中,很實用,大家多多揣摩處工作過程中的各種場景。

快速查詢兄弟字串

如果兩個字串中的字元一樣,出現的次數也一樣,只是出現的順序不一樣,那麼他們就是兄弟字串,比如 abc 和 bca 要求 在字典中查詢兄弟字串?思路 1.現將字串進行排序 2.排序後的字串是否對應相等 1 include 2 include 3 include 4 using namespace st...

python在字串中查詢字元

兩類函式 find rfind index rindex 找到了都返回下標.find找不到返回 1,index找不到丟擲valueerror.帶r的表示從右向左找.都可以使用第二個引數表示從哪個下標開始找.a abcdab a.find a out 3 0 a.rfind a out 4 4a.rf...

查詢字串

在乙個主串中查詢相應的子串,如 abcdwoshidef 中查詢 woshi 方法 該實現的方法是最簡單的模式匹配方法,時間複雜度較高 include iostream using namespace std int searchstring const char str1,const char s...