在python中截断或修剪列表

72qzrwbm  于 2023-02-17  发布在  Python
关注(0)|答案(5)|浏览(118)

我列了一个清单:

[{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '10.113.97.0/26'}, {'CidrIp': '10.111.0.0/22'}, {'CidrIp': '10.140.0.0/16'}, {'CidrIp': '10.141.0.0/16'}, {'CidrIp': '10.145.0.0/16'}, {'CidrIp': '10.112.0.0/26'}, {'CidrIp': '10.112.0.64/26'}], 'Ipv6Ranges': [], 'PrefixListIds': [], 'UserIdGroupPairs': []}]

仅需修剪单词CidrIP后的IP,仅需修剪数字:
10.113.97.0/26 10.111.0.0/22 10.140.0.0/16 ...
这怎么可能?我用试纸或标测图测试过了,但没有结果。
多谢了!

dluptydi

dluptydi1#

ips = data.get('IpRanges', []) # get the IpRanges list from main dict
ips = [ip['CidrIp'] for ip in ips] # iterate over Ipranges list and get value of CidrIp.
print(ips)
eeq64g8w

eeq64g8w2#

你可以结合使用列表解析和字符串操作来从字典列表中只提取IP地址。下面是一个应该可以工作的示例代码:

data = [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '10.113.97.0/26'}, {'CidrIp': '10.111.0.0/22'}, {'CidrIp': '10.140.0.0/16'}, {'CidrIp': '10.141.0.0/16'}, {'CidrIp': '10.145.0.0/16'}, {'CidrIp': '10.112.0.0/26'}, {'CidrIp': '10.112.0.64/26'}], 'Ipv6Ranges': [], 'PrefixListIds': [], 'UserIdGroupPairs': []}]

ip_list = [ip['CidrIp'].split('/')[0] for item in data for ip in item['IpRanges']]
print(ip_list)

这将返回以下内容:

['10.113.97.0', '10.111.0.0', '10.140.0.0', '10.141.0.0', '10.145.0.0', '10.112.0.0', '10.112.0.64']
l2osamch

l2osamch3#

您可以使用以下代码:

AIM = [i["CidrIp"] for i in DATA[0].get("IpRanges")]
qlfbtfca

qlfbtfca4#

您可以使用一个for循环来使用具有线性时间复杂度的列表解析

l=[{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '10.113.97.0/26'}, {'CidrIp': '10.111.0.0/22'}, {'CidrIp': '10.140.0.0/16'}, {'CidrIp': '10.141.0.0/16'}, {'CidrIp': '10.145.0.0/16'}, {'CidrIp': '10.112.0.0/26'}, {'CidrIp': '10.112.0.64/26'}], 'Ipv6Ranges': [], 'PrefixListIds': [], 'UserIdGroupPairs': []}]

ips=[x['CidrIp'] for x in l[0]['IpRanges']]

print(ips)
['10.113.97.0/26', '10.111.0.0/22', '10.140.0.0/16', '10.141.0.0/16', '10.145.0.0/16', '10.112.0.0/26', '10.112.0.64/26']

如果你正在寻找这个:

ips=[x['CidrIp'].split('/')[0] for x in l[0]['IpRanges']]
print(ips)
['10.113.97.0', '10.111.0.0', '10.140.0.0', '10.141.0.0', '10.145.0.0', '10.112.0.0', '10.112.0.64']
kh212irz

kh212irz5#

假设数据对象存储在名为data的变量中

data = [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '10.113.97.0/26'}, {'CidrIp': '10.111.0.0/22'}, {'CidrIp': '10.140.0.0/16'}, {'CidrIp': '10.141.0.0/16'}, {'CidrIp': '10.145.0.0/16'}, {'CidrIp': '10.112.0.0/26'}, {'CidrIp': '10.112.0.64/26'}], 'Ipv6Ranges': [], 'PrefixListIds': [], 'UserIdGroupPairs': []}]

现在,使用列表解析提取所有IP地址

ip_address_list = [ip['CidrIp'] for ip in data[0]['IpRanges']]

输出结果为

['10.113.97.0/26', '10.111.0.0/22', '10.140.0.0/16', '10.141.0.0/16', '10.145.0.0/16', '10.112.0.0/26', '10.112.0.64/26']

如果只想获取地址

ip_address_list = [ip['CidrIp'].split('/')[0] for ip in data[0]['IpRanges']]

结果是

['10.113.97.0', '10.111.0.0', '10.140.0.0', '10.141.0.0', '10.145.0.0', '10.112.0.0', '10.112.0.64']

如果只想获取子网掩码

subnet_mask_list = [int(ip['CidrIp'].split('/')[1]) for ip in data[0]['IpRanges']]

结果是

[26, 22, 16, 16, 16, 26, 26]

您还可以使用ipaddress库来解析IP地址,并将其分离为地址和子网掩码

import ipaddress

for ip in data[0]['IpRanges']:
     net = ipaddress.ip_network(ip['CidrIp'])
     print('Address :', net.network_address, ' Subnet Mask :', net.netmask)

输出结果

Address : 10.113.97.0  Subnet Mask : 255.255.255.192
Address : 10.111.0.0  Subnet Mask : 255.255.252.0
Address : 10.140.0.0  Subnet Mask : 255.255.0.0
Address : 10.141.0.0  Subnet Mask : 255.255.0.0
Address : 10.145.0.0  Subnet Mask : 255.255.0.0
Address : 10.112.0.0  Subnet Mask : 255.255.255.192
Address : 10.112.0.64  Subnet Mask : 255.255.255.192

相关问题