user1596466
user1596466

Reputation: 57

Python: Return string if substring in string

I have two lists one has a bunch of paths ex: "C:\vol001\ABC123.xls" and the other has a bunch of names ex: "ABC123". I'm looking to find the paths if the name is in the path. So for the above example, the program would spit out "C:\vol001\ABC123.xls".

So far I've been trying to use the in operator, but I'm not sure how to return the path.

for name in list1:
    if name in list2:
        return path in list2

Upvotes: 2

Views: 3149

Answers (5)

Hyperboreus
Hyperboreus

Reputation: 32459

Taking into consideration your comment, that it doesn't matter where in the path the name appears (i.e. both "C:\ABC123\test.png" and "C:\vol01\ABC123.xml" match the query "ABC123"), we do not take into consideration the structure of the file system paths, but can work on pure string matching.

Be paths the list of all paths, be names the list of all names, and be name the name you are looking for then

(path for path in paths if name in path)

is the generator yielding all paths matching name.

dict ( ( (name, [path for path in paths if name in path] ) for name in names) )

creates a dictionary whose keys are the names and whose values are all paths that contain the corresponding name. You can use it someway like that:

paths = [....]
names = [....]
d = dict ( ( (name, [path for path in paths if name in path] ) for name in names) )
print ('The name "ABC" is contained in: {}'.format (d ['ABC'] ) )
print ('The name "XYZ" is contained in: {}'.format (d ['XYZ'] ) )
print ('The name "pron" is contained in: {} different paths'.format (len (d ['pron'] ) if 'pron' in d else 0) )

Upvotes: 0

jackcogdill
jackcogdill

Reputation: 5132

Does this work for you?

new_paths = []                          # create list for the paths to be kept
for path in paths:                      # cycle through paths
    for name in names:                  # cycle through names
        if name in path:                # check if path contains the name
            new_paths.append(path)      # path is good, so add to the list

Upvotes: 0

martineau
martineau

Reputation: 123541

I think this is what you want:

for path in list1:
    for name in list2:
        if name in path:
            return path
else:
    return None # not found

Upvotes: 0

Alex G.P.
Alex G.P.

Reputation: 10028

Your initial solution is not correct. Actually, you are asking "is there 'name' equal one of the item in the list". But if you would like to test for containment, i.e. "whether 'name' is part of any item of the list" you should use something like code below:

for name in l2:
    return [x for x in l1 if name in x]

It is returns list of matched pathes. Actually it is just 2 for cycles rewritten in more pythonic way (as I remember it is called 'list comprehension', people who more familiar with terminology fixes me if I am wrong).

Upvotes: 2

Elias Zamaria
Elias Zamaria

Reputation: 101193

return os.path.dirname(name)

Keep in mind that you have to run import os.path if you haven't already. See the documentation for the os.path module.

Upvotes: -1

Related Questions