来自大量电话号码的电话号码是另一个电话号码的前号码?

时间:2015-03-02 09:35:51

标签: python python-2.7

Example_List=[112,34533344,11234543,98]

在上面的列表中,我们可以看到11211234543的预编号。我怎么能在python中检查这个?我认为re.matchre.search是其中一种解决方案吗?

3 个答案:

答案 0 :(得分:0)

您可以将数字转换为字符串然后对其进行排序。 之后,如果一个号码是一个前号码,那么所有以它开头的号码都将跟随,一旦一个号码不再以它开头,你就找到了所有号码。

Example_List=[112,34533344,11234543,98]
list_s = sorted(str(i) for i in Example_List)

result = []
for i in range(len(list_s)):
    k=i+1
    while k<len(list_s) and list_s[k].startswith(list_s[i]):
        result.append((list_s[i],list_s[k]))
        k += 1

print(result)

答案 1 :(得分:0)

如果您想查找所有号码,从列表中的任何数字开始,您可以执行以下操作:


#Converte your list of ints to list of strings
tmp = map(lambda x : str(x),l)
#Use a set so you duplicate entries are eliminated
result = set()
for x in tmp:
    #Insert all numbers starting with the current one
    result.update([y for y in filter(lambda z : z.startswith(x) and z != x, l)])
#Convert your set of ints to a list of strings
result = list(map(int, result))

答案 2 :(得分:0)

单个列表理解将返回列表中的所有数字(以整数形式),这些数字以指定的&#34;前编号&#34;开头:

In [1]: Example_List=[112,34533344,11234543,98]
In [2]: [num for num in Example_List if str(num).startswith('112')]
Out[2]: [112, 11234543]

如果您的预编号始终是列表中的第一个条目,但对于不同的列表可能会有所不同,那么您仍然可以在一行中执行此操作,但需要更长时间:

In [3]: [num for num in Example_List[1:] if str(num).startswith(str(Example_List[0]))]
Out[3]: [11234543]