Created
December 4, 2017 07:44
-
-
Save toaco/777abe84b9f78c7a59fb84f158d1344e to your computer and use it in GitHub Desktop.
判断国内电话号码运营商,将多个电话号码按照运营商分组
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
class groupby2(object): | |
""" | |
[k for k, g in groupby2('AAAABBBCCDAABBB')] --> A B C D | |
""" | |
def __init__(self, iterable, key=lambda x: x): | |
self.key_func = key | |
self.iterable = iter(iterable) | |
def __iter__(self): | |
result = collections.defaultdict(list) | |
for elem in self.iterable: | |
key = self.key_func(elem) | |
result[key].append(elem) | |
for key, elem in result.iteritems(): | |
yield key, elem | |
class ServiceProvider(object): | |
""" | |
reference: https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%9B%BD%E5%86%85%E5%9C%B0%E7%A7%BB%E5%8A%A8%E7%BB%88%E7%AB%AF%E9%80%9A%E8%AE%AF%E5%8F%B7%E7%A0%81 | |
date: 2017-12-04 13:31:47 CST | |
""" | |
MOBILE = set(range(1340, 1349)) | { | |
135, 136, 137, 138, 139, 1440, 147, 148, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, | |
187, 188, 198, | |
} | {147} | {1703, 1705, 1706} | |
UNICOM = {130, 131, 132, 145, 146, 155, 156, 175, 176, 185, 186, } | {145} | { | |
1704, 1707, 1708, 1709, 171 | |
} | |
TELECOM = set(range(17400, 17406)) | { | |
133, 1349, 1410, 149, 153, 173, 177, 180, 181, 189, 199 | |
} | {149} | |
def __init__(self): | |
self.phone_sp = {} | |
for value in self.MOBILE: | |
self.phone_sp[value] = 'mobile' | |
for value in self.UNICOM: | |
self.phone_sp[value] = 'unicom' | |
for value in self.TELECOM: | |
self.phone_sp[value] = 'telecom' | |
def get_sp_by_phone_number(self, number_prefix): | |
number_prefix = str(int(number_prefix)) | |
if int(number_prefix[:3]) in self.phone_sp: | |
return self.phone_sp[int(number_prefix[:3])] | |
if int(number_prefix[:4]) in self.phone_sp: | |
return self.phone_sp[int(number_prefix[:4])] | |
else: | |
return 'unknown' | |
if __name__ == '__main__': | |
a = [ | |
130064, 130081, 130182, 2133113, 133164, 133209, 133227, 133309, 133367, 134419, 138992, | |
138995, 139067, 139081, 786645, 786668, 786828 | |
] | |
key_func = ServiceProvider().get_sp_by_phone_number | |
for i, k in groupby2(a, key=lambda x: key_func(x)): | |
print i, len(k), list(k) | |
# telecom 5 [133164, 133209, 133227, 133309, 133367] | |
# unknown 4 [2133113, 786645, 786668, 786828] | |
# unicom 3 [130064, 130081, 130182] | |
# mobile 5 [134419, 138992, 138995, 139067, 139081] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment