我有一個(gè)這樣的列表:['10.131.0.240/29-NameoftheNetwork-8-8-0', '10.121.0.248/29-NameoftheNetwork2-8-8-0', '10.111.2.248/29-NameoftheNetwork3-8-7-1']每個(gè)值的最后三個(gè)數(shù)字是:IP 總數(shù)/已用 IP/剩余 IP。我只想為每個(gè)網(wǎng)絡(luò)獲取“左 IP”,將其轉(zhuǎn)換為:['10.131.0.240/29-NameoftheNetwork-8-8-0', '10.121.0.248/29-NameoftheNetwork2-8-8-0', '10.111.2.248/29-NameoftheNetwork3-8-7-1']像這樣:['10.131.0.240/29-NameoftheNetwork=0', '10.121.0.248/29-NameoftheNetwork2=0', '10.111.2.248/29-NameoftheNetwork3=1']但是我沒有找到一種方法來刪除我不想要的字符,而且我沒有重復(fù)替換它的模式或“=”。
1 回答

一只斗牛犬
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
使用正則表達(dá)式。
前任:
import re
data = ['10.131.0.240/29-NameoftheNetwork-8-8-0', '10.121.0.248/29-NameoftheNetwork2-8-8-0', '10.111.2.248/29-NameoftheNetwork3-8-7-1']
data = [re.sub(r"(.*?)\-\d+\-\d+\-(\d+)", r"\1=\2", i) for i in data]
print(data)
輸出:
['10.131.0.240/29-NameoftheNetwork=0', '10.121.0.248/29-NameoftheNetwork2=0', '10.111.2.248/29-NameoftheNetwork3=1']
添加回答
舉報(bào)
0/150
提交
取消