我有一个这样的列表:['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']每个值的最后三个数字是:IP 总数/已用 IP/剩余 IP。我只想为每个网络获取“左 IP”,将其转换为:['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']但是我没有找到一种方法来删除我不想要的字符,而且我没有重复替换它的模式或“=”。
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
使用正则表达式。
前任:
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']
添加回答
举报
0/150
提交
取消