Skip to content
Advertisement

Python finding the common parts of a string throughout a list and removing it from every item

I have a list of file directories that looks similar to this:

JavaScript

I am trying to remove the beginnings of the paths that are the same from every list, and then deleting that from each file.

The list can be any length, and in the example I would be trying to change the list into:

JavaScript

Methods like re.sub(r'.*I', 'I', filepath) and filepath.split('_', 1)[-1] can be used for the replacing, but I’m not sure about how to find the common parts in the list of filepaths

Note:

I am using Windows and python 3

Advertisement

Answer

The first part of the answer is here: Python: Determine prefix from a set of (similar) strings

Use os.path.commonprefix() to find the longest common (first part) of the string

The code for selecting the part of the list that is the same as from that answer is:

JavaScript

Now all you have to do is use slicing to remove the resulting string from each item in the list

This results in:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement