Ben10
Ben10

Reputation: 297

Verify that a key is a substring of another keys in a dictionary

I am new to python and I want to know how to find out that a key is a substring of another keys in a dictionary. Example that I have a dictionary:

dictionary = {'modulus of elasticity': 24, 'cross validation': 16, 'mechanical properties': 16, 'elasticity': 2}

The code I have tried:

for k in dictionary.keys():
    res = [ d for d in dictionary.keys() if d in k ]
    if res:
        if(len(d) > len(k)):
            print(d,"is longer than ", k)
    else:
        pass

For my code I know it will get error that NameError: name 'd' is not defined as in fact I dont know how to capture the value of k and d when there is a substring between them. In my case I want to loop through the key in the dictionary and verified that if a current key is the substring of another key in the whole dictionary. For example, if the first key like modulus of elasticity I want to check that the other keys beside itself is containing it or not until the end of dictionary and of course when we loop until elasticity we will see that elasticity is the substring of modulus of elasticity but not vice-versa. In this case I want to capture both the key elasticity and key modulus of elasticity to print in the if statement: modulus of elasticity is longer than elasticity or in the other cases, I want to also manipulate their values. How to do this? Any help would be much appreciated! Thanks!

Upvotes: 0

Views: 76

Answers (3)

Iron Fist
Iron Fist

Reputation: 10951

You don't have to scan from the beginning of the list each loop iteration, this is just extra CPU cycles, instead, sort the list by keys'length, then on each iteration start from the next item of the current iteration value, as per the following code:

>>> keys = sorted(dictionary.keys(), key=lambda k: len(k))
>>> 
>>> for index, key_out in enumerate(keys):
        for key_in in keys[index+1:]:
            if (key_out in key_in) and (key_out != key_in):
                print('[' + key_out + ']' + ' is a substring of [' + key_in + ']')

            
[elasticity] is a substring of [modulus of elasticity]

Upvotes: 1

Vasilis G.
Vasilis G.

Reputation: 7846

What you need to do is:

  1. Get all keys from dictionary.

  2. For each key, search to see if it's a substring of another key. If this is the case, print it. Else move on to the next one.

    keys = list(dictionary.keys())
    for key in keys:
        for other_key in keys:
            if key == other_key:
                continue
            else:
                if key in other_key:
                    print(key + ' is a substring of ' + other_key)
    

Upvotes: 2

marcos
marcos

Reputation: 4510

d is only defined inside the list comprehension, here is a simpler way to accomplish that:

dictionary = {'modulus of elasticity': 24, 'cross validation': 16, 'mechanical properties': 16, 'elasticity': 2}


for k in dictionary.keys():
    for k2 in dictionary.keys():
        if k != k2 and k in k2: print(k2 ,"is longer than ", k)

>>> modulus of elasticity is longer than  elasticity

We loop two times through the keys of the dictionary and we check if they are not the same key, otherwise we validate if one is a substring of the other one.

Upvotes: 1

Related Questions