我的脚本有一点问题,我需要将表单'xxx.xxx.xxx.xxx'中的ip转换为整数表示,然后从此表单返回.
def iptoint(ip):
return int(socket.inet_aton(ip).encode('hex'),16)
def inttoip(ip):
return socket.inet_ntoa(hex(ip)[2:].decode('hex'))
In [65]: inttoip(iptoint('192.168.1.1'))
Out[65]: '192.168.1.1'
In [66]: inttoip(iptoint('4.1.75.131'))
---------------------------------------------------------------------------
error Traceback (most recent call last)
/home/thc/<ipython console> in <module>()
/home/thc/<ipython console> in inttoip(ip)
error: packed IP wrong length for inet_ntoa`
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
我有一个元组列表,每个元组都是一个元组(start-time, end-time).我正在尝试合并所有重叠的时间范围并返回不同时间范围的列表.例如
[(1, 5), (2, 4), (3, 6)] ---> [(1,6)]
[(1, 3), (2, 4), (5, 8)] ---> [(1, 4), (5,8)]
Run Code Online (Sandbox Code Playgroud)
这是我实现它的方式.
# Algorithm
# initialranges: [(a,b), (c,d), (e,f), ...]
# First we sort each tuple then whole list.
# This will ensure that a<b, c<d, e<f ... and a < c < e ...
# BUT the order of b, d, f ... is still random
# Now we have only 3 possibilities
#================================================
# b<c<d: a-------b Ans: …Run Code Online (Sandbox Code Playgroud) 假设我有一个包含索引的列表列表[[start, end], [start1, end1], [start2, end2]].
例如:
[[0, 133], [78, 100], [25, 30]].
如何检查列表之间的重叠并删除每次更长的列表?所以:
>>> list = [[0, 133], [78, 100], [25, 30]]
>>> foo(list)
[[78, 100], [25, 30]]
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止尝试做的事情:
def cleanup_list(list):
i = 0
c = 0
x = list[:]
end = len(x)
while i < end-1:
for n in range(x[i][0], x[i][1]):
if n in range(x[i+1][0], x[i+1][1]):
list.remove(max(x[i], x[i+1]))
i +=1
return list
Run Code Online (Sandbox Code Playgroud)
但除了令人费解之外它还没有正常工作:
>>>cleanup_list([[0,100],[9,10],[12,90]])
[[0, 100], [12, 90]]
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
编辑:
其他例子是:
>>>a = [[0, 100], …Run Code Online (Sandbox Code Playgroud) netaddr.cidr_merge我有一个 IP 网络列表,即使其中一些是相邻的,也不会合并。难道我做错了什么?
>>> from netaddr import IPNetwork, cidr_merge
>>> iplist = [
IPNetwork('10.105.205.8/29'),
IPNetwork('10.105.205.16/28'),
IPNetwork('10.105.205.32/27'),
IPNetwork('10.105.205.64/26'),
IPNetwork('10.105.205.128/26'),
IPNetwork('10.105.205.192/28'),
IPNetwork('10.105.205.208/29'),
IPNetwork('10.105.206.48/28'),
IPNetwork('10.105.206.80/28')
]
>>> summary = cidr_merge(iplist)
>>> summary == iplist
True
Run Code Online (Sandbox Code Playgroud)
我在 Mac OSX 10.8.5 上使用 Python 3.3.4 和 netaddr 7.12。