=Start=
缘由:
同事需要实现一个功能:从指定IP段中排除某些IP或IP段,最后得到一个类似于IP区间的结果格式。比如:
$ python py_exclude_ip_cidr.py 10.32.0.0/16 10.32.0.0/24 10.32.26.254 10.32.104.199 10.32.1.0-10.32.26.253 10.32.26.255-10.32.104.198 10.32.104.200-10.32.255.255
正文:
理清思路:
将CIDR格式的IP段转换成大整数区间的格式,然后再从这个大区间中剔除「要排除的小区间」,然后将区间的首尾整数转换成IP地址的格式。
提取出的问题:
其中碰到的一个问题就是,将一个「不一定连续」的有序列表转换成「一个或多个」连续有序列表。
参考解答:
实现一个函数myfunc,传入不一定连续的列表,返回一个包含多个区间的列表,比如:
myfunc([2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 20])
返回:
[(2,5), (12,17), 20]
==
from operator import itemgetter from itertools import groupby ranges = [] data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17] for k, g in groupby(enumerate(data), lambda (i,x):i-x): group = map(itemgetter(1), g) ranges.append((group[0], group[-1])) print group print ranges
>>> # Show a dictionary sorted and grouped by value(Python中根据dict中元素出现的次数进行分组) >>> from operator import itemgetter >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) >>> di = sorted(d.iteritems(), key=itemgetter(1)) >>> for k, g in groupby(di, key=itemgetter(1)): ... print k, map(itemgetter(0), g) ... 1 ['a', 'c', 'e'] 2 ['b', 'd', 'f'] 3 ['g'] >>> # Find runs of consecutive numbers using groupby. The key to the solution >>> # is differencing with a range so that consecutive numbers all appear in >>> # same group.(将list中连续的串进行分组) >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): ... print map(itemgetter(1), g) ... [1] [4, 5, 6] [10] [15, 16, 17, 18] [22] [25, 26, 27, 28]
参考链接:
- http://stackoverflow.com/questions/2154249/identify-groups-of-continuous-numbers-in-a-list
- http://stackoverflow.com/questions/2361945/detecting-consecutive-integers-in-a-list
- http://stackoverflow.com/questions/16315189/python-find-incremental-numbered-sequences-with-a-list-comprehension
- http://stackoverflow.com/questions/3429510/pythonic-way-to-convert-a-list-of-integers-into-a-string-of-comma-separated-rang
- http://stackoverflow.com/questions/10420464/group-list-of-ints-by-continuous-sequence
- http://stackoverflow.com/questions/33402355/finding-groups-of-increasing-numbers-in-a-list
- https://docs.python.org/2.6/library/itertools.html#examples
=END=
《“如何用Python分割不连续的有序列表”》 有 1 条评论
Python中如何根据字典的value进行排序:
`
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
import operator
sorted_x = sorted(d.items(), key=operator.itemgetter(1))
#或
sorted(d.items(), key=lambda x: x[1])
`
http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
http://stackoverflow.com/questions/16772071/sort-dict-by-value-python