将数字序列折叠为范围

时间:2018-09-12 19:36:12

标签: python python-3.x pandas

今天,我正在寻求有关正在编写的Python脚本的帮助;我正在使用CSV模块来解析大约1100行的大型文档,并且从每一行中提取一个Case_ID,这是其他行没有的唯一编号。例如:

['10215', '10216', '10277', '10278', '10279', '10280', '10281', '10282', '10292', '10293',
'10295', '10296', '10297', '10298', '10299', '10300', '10301', '10302', '10303', '10304',
'10305', '10306', '10307', '10308', '10309', '10310', '10311', '10312', '10313', '10314',
'10315', '10316', '10317', '10318', '10319', '10320', '10321', '10322', '10323', '10324',
'10325', '10326', '10344', '10399', '10400', '10401', '10402', '10403', '10404', '10405',
'10406', '10415', '10416', '10417', '10418', '10430', '10448', '10492', '10493', '10494',
'10495', '10574', '10575', '10576', '10577', '10578', '10579', '10580', '10581', '10582',
'10583', '10584', '10585', '10586', '10587', '10588', '10589', '10590', '10591', '10592',
'10593', '10594', '10595', '10596', '10597', '10598', '10599', '10600', '10601', '10602',
'10603', '10604', '10605', '10606', '10607', '10608', '10609', '10610', '10611', '10612',
'10613', '10614', '10615', '10616', '10617', '10618', '10619', '10620', '10621', '10622',
'10623', '10624', '10625', '10626', '10627', '10628', '10629', '10630', '10631', '10632',
'10633', '10634', '10635', '10636', '10637', '10638', '10639', '10640', '10641', '10642',
'10643', '10644', '10645', '10646', '10647', '10648', '10649', '10650', '10651', '10652',
'10653', '10654', '10655', '10656', '10657', '10658', '10659', '10707', '10708', '10709',
'10710', '10792', '10793', '10794', '10795', '10908', '10936', '10937', '10938', '10939',
'11108', '11109', '11110', '11111', '11112', '11113', '11114', '11115', '11116', '11117',
'11118', '11119', '11120', '11121', '11122', '11123', '11124', '11125', '11126', '11127',
'11128', '11129', '11130', '11131', '11132', '11133', '11134', '11135', '11136', '11137',
'11138', '11139', '11140', '11141', '11142', '11143', '11144', '11145', '11146', '11147',
'11148', '11149', '11150', '11151', '11152', '11153', '11154', '11155', '11194', '11195',
'11196', '11197', '11198', '11199', '11200', '11201', '11202', '11203', '11204', '11205',
'11206', '11207', '11208', '11209', '11210', '11211', '11212', '11213', '11214', '11215',
'11216', '11217', '11218', '11219', '11220', '11221', '11222', '11223', '11224', '11225',
'11226', '11227', '11228', '11229', '11230', '11231', '11232', '11233', '11234', '11235',
'10101', '10102', '10800', '11236']

如您所见,该列表非常直观,因此我想在脚本中包含一个小的小函数,该函数可以将所有顺序范围缩小为某种带连字符的书挡,例如10,277-10,282 。

感谢所有人提供的任何帮助!祝你有美好的一天。

3 个答案:

答案 0 :(得分:7)

可行。让我们看看这是否可以用熊猫来完成。

import pandas as pd

data = ['10215', '10216', '10277', ...]
# Load data as series.
s = pd.Series(data)
# Find all consecutive rows with a difference of one 
# and bin them into groups using `cumsum`. 
v = s.astype(int).diff().bfill().ne(1).cumsum() 
# Use `groupby` and `apply` to condense the consecutive numbers into ranges.
# This is only done if the group size is >1.
ranges = (
    s.groupby(v).apply(
        lambda x: '-'.join(x.values[[0, -1]]) if len(x) > 1 else x.item()).tolist())

print (ranges)
['10215-10216',
 '10277-10282',
 '10292-10293',
 '10295-10326',
 '10344',
 '10399-10406',
 '10415-10418',
 '10430',
 '10448',
 '10492-10495',
 '10574-10659',
 '10707-10710',
 '10792-10795',
 '10908',
 '10936-10939',
 '11108-11155',
 '11194-11235',
 '10101-10102',
 '10800',
 '11236']

您的数据必须经过排序才能正常工作。

答案 1 :(得分:1)

您可以在此处使用具有以下逻辑的简单循环:

  1. 创建一个列表来存储范围(ranges)。
  2. 遍历列表(l)中的值
  3. 如果ranges为空,请将l中第一个值的列表附加到ranges
  4. 否则,如果当前值和上一个值之间的差为1,则将当前值附加到ranges中的最后一个列表中
  5. 否则,将具有当前值的列表附加到ranges

代码:

l = ['10215', '10216', '10277', '10278', '10279', '10280', ...]

ranges = []
for x in l:
    if not ranges:
        ranges.append([x])
    elif int(x)-prev_x == 1:
        ranges[-1].append(x)
    else:
        ranges.append([x])
    prev_x = int(x)

现在,您可以通过串联ranges中每个列表的第一个和最后一个元素(如果至少有两个元素)来计算最终范围。

final_ranges = ["-".join([r[0], r[-1]] if len(r) > 1 else r) for r in ranges]
print(final_ranges)
#['10215-10216',
# '10277-10282',
# '10292-10293',
# '10295-10326',
# '10344',
# '10399-10406',
# '10415-10418',
# '10430',
# '10448',
# '10492-10495',
# '10574-10659',
# '10707-10710',
# '10792-10795',
# '10908',
# '10936-10939',
# '11108-11155',
# '11194-11235',
# '10101-10102',
# '10800',
# '11236']

这还假定您的数据已排序。您可以简化代码以结合第3项和第5项。


出于纯粹的教育目的(这比上面的循环效率低得多),这是使用mapreduce的同一件事:

from functools import reduce

def myreducer(ranges, x):
    if not ranges:
        return [[x]]
    elif (int(x) - int(ranges[-1][-1]) == 1):
        return ranges[:-1] + [ranges[-1]+[x]] 
    else:
        return ranges + [[x]]

final_ranges = map(
    lambda r: "-".join([r[0], r[-1]] if len(r) > 1 else r),
    reduce(myreducer, l, [])
)

答案 2 :(得分:0)

还有pynumparser软件包:

import pynumparser         

pynumparser.NumberSequence().encode([1, 2, 3, 5, 6, 7, 8, 10])
# result: '1-3,5-8,10'

pynumparser.NumberSequence().parse('1-3,5-8,10')              
# result: (1, 2, 3, 5, 6, 7, 8, 10)