简体   繁体   English

Python 从列表中删除类似项目

[英]Python remove similar items from list

Using python, in the following list I need to remove phone numbers which are repeated with country codes.使用 python,在下面的列表中,我需要删除与国家代码重复的电话号码。

['(+44)45860787163','(+27)16345860787','45860787163','16345860787']

I tried using Cartesian product and 'in' operator to compare strings but nothing seems to be working.我尝试使用笛卡尔积和“in”运算符来比较字符串,但似乎没有任何效果。 What I'd like to keep are the full phone numbers.我想保留完整的电话号码。

['(+44)45860787163','(+27)16345860787']

Using a regex you may extract the part you need, then using a dict, you can pair the number with its prefix (and avoid overwriting a prefix)使用正则表达式,您可以提取您需要的部分,然后使用字典,您可以将数字与其前缀配对(并避免覆盖前缀)

value = '(+44)45860787163,(+27)16345860787,45860787163,16345860787'
phones = {}

for phone, prefix, number in re.findall(r"((\(\+\d+\))?(\d+))", value):
    if prefix != "" or number not in phone:
        phones[number] = prefix

result = ",".join(v + k for k, v in phones.items())
print(result)  # (+44)45860787163,(+27)16345860787

Using list comprehension使用列表理解

l = ['(+44)45860787163','(+27)16345860787','45860787163','16345860787']

l = [x for x in l if '(' in x]

print(l)

>>> ['(+44)45860787163', '(+27)16345860787']

If you want to be extra secure you can check for both semi colons如果您想更加安全,可以检查两个分号

l = [x for x in l if '(' in x and ')' in x]

First of all, we get all the phone numbers.首先,我们得到所有的电话号码。 Then, we create a dictionary with number without extension as key and extension as value.然后,我们创建一个字典,其中没有扩展名的数字作为键,扩展名作为值。 If a number hasn't extension it's set to None.如果一个号码没有分机,则设置为无。 After, if we find the same number with extension None is replaced by the extension.之后,如果我们发现与扩展名相同的号码 None 被扩展名替换。 At the end, we print the number with the corresponding extension.最后,我们打印带有相应扩展名的号码。

numbers = '(+44)45860787163,(+27)16345860787,45860787163,16345860787'.split(',')
d = dict()
for number in numbers:
    if ')' in number:
        prefix, n = number.split(')')
        prefix = prefix + ')'
    else:
        prefix = None
        n = number
    if n in d.keys():
        if d[n] == None:
            d[n] = prefix
    else:
        d[n] = prefix
print([k if v == None else v + k for k,v in d.items()])

You can use this solution:您可以使用此解决方案:

number_string = '(+44)45860787163,(+27)16345860787,45860787163,16345860787'

result = []

for phone_number in number_string.split(','):
    if "(" in phone_number:
        result.append(phone_number)

print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM