Python从列表中删除常见单词

时间:2018-07-19 07:19:17

标签: python

我需要从列表中删除常用词。需要删除的单词是IPNetwork

IP_list = [IPNetwork('10.60.252.0/23'),
 IPNetwork('10.60.254.0/23'),
 IPNetwork('10.208.0.0/15'),
 IPNetwork('10.208.64.80/28'),
 IPNetwork('10.208.152.0/24'),
 IPNetwork('10.208.153.0/24'),
 IPNetwork('10.208.154.0/24'),
 IPNetwork('10.208.155.128/25'),
 IPNetwork('10.208.156.0/24')]

预期结果:

['10.60.252.0/23',
 '10.60.254.0/23',
 '10.208.0.0/15',
 '10.208.64.80/28',
 '10.208.152.0/24',
 '10.208.153.0/24',
 '10.208.154.0/24',
 '10.208.155.128/25',
 '10.208.156.0/24']

1 个答案:

答案 0 :(得分:3)

IPNetwork是一个类,您正在实例化它的对象。如果您只希望IP地址采用字符串格式,请将其明确转换为字符串

使用map

>>> list(map(str, IP_list))
['10.60.252.0/23', '10.60.254.0/23', '10.208.0.0/15', '10.208.64.80/28', '10.208.152.0/24', '10.208.153.0/24', '10.208.154.0/24', '10.208.155.128/25', '10.208.156.0/24']

或使用列表理解

>>> [str(ip) for ip in IP_list]
['10.60.252.0/23', '10.60.254.0/23', '10.208.0.0/15', '10.208.64.80/28', '10.208.152.0/24', '10.208.153.0/24', '10.208.154.0/24', '10.208.155.128/25', '10.208.156.0/24']