我有一个电话号码范围,例如:
3331234-3332345
我需要编写一个函数将其转换为前缀列表:
3331234
...
3331239
333124
...
333129
33313
...
33319
33320
...
33322
333231
333232
333233
3332341
...
3332345
问题没那么简单。我不需要获取范围开始和结束之间的数字列表。
最佳答案
我的工作代码。它也不是很快。欢迎优化。
def diap_to_prefix(a, b):
lst = ['%0*d'%(max(len(str(a)), len(str(b))), x) for x in range(int(a), int(b)+1)]
new_lst = []
while len(lst) != len(new_lst):
lst = new_lst or lst
new_lst = []
c = lst[0]
tmp_lst = [c]
for i in lst[1:]:
if c[:-1] == i[:-1]:
c = i
tmp_lst.append(c)
else:
if len(tmp_lst) == 10:
new_lst.append(c[:-1])
else:
new_lst.extend(tmp_lst)
c = i
tmp_lst = [c]
if len(tmp_lst) == 10:
new_lst.append(c[:-1])
else:
new_lst.extend(tmp_lst)
return lst
关于python - 将电话号码范围列表转换为前缀列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165629/