What is the most efficient way to check if a value of a list is contained in one of the other values of the list?

nadavfr31 :

Lately I have tried to solve a problem that I have in my code that is about checking if a value of a list is contained in other value of the same list. My specific list contains phone numbers, so it looks like that:

['972526174656' , '526174656' , '174656'] 

but the way I want it to look like is that:

['972526174656']

I have tried to solve it with the easiest way of doing for inside another for:

phones_list = ['972526174656' , '526174656' , '174656']
drop_list = []
for phone in phones_list:
    for phone2 in phones_list:
        if phone2.contains(phone) and phone2!=phone:
           drop_list.append(phone)
phones_list = list(set(phones_list) - set(drop_list))

Although this example works, its efficiency is bad and in my original data I have 3,000,000 cases like the one I showed you. So efficiency is the key for my code to succeed!

I hope the community could help me to solve this. Thank you very much for any kind of help!

quamrana :

You can use itertools.permutations:

phones_list = ['972526174656' , '526174656' , '174656']
drop_list = []

for p1,p2 in itertools.permutations(phones_list, 2):
    if p1 in p2:
        drop_list.append(p1)

phones_list = list(set(phones_list) - set(drop_list))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=298141&siteId=1