6

I was wondering what's the most pythonic way to:

Having a list of strings and a list of substrings remove the elements of string list that contains any of the substring list.

list_dirs = ('C:\\foo\\bar\\hello.txt', 'C:\\bar\\foo\\.world.txt', 'C:\\foo\\bar\\yellow.txt')

unwanted_files = ('hello.txt', 'yellow.txt)

Desired output:

list_dirs = (C:\\bar\\foo\.world.txt')

I have tried to implement similar questions such as this, but I'm still struggling making the removal and extend that particular implementation to a list.

So far I have done this:

for i in arange(0, len(list_dirs)):
    if 'hello.txt' in list_dirs[i]:
        list_dirs.remove(list_dirs[i])

This works but probably it's not the more cleaner way and more importantly it does not support a list, if I want remove hello.txt or yellow.txt I would have to use a or. Thanks.

Community
  • 1
  • 1
dudas
  • 403
  • 6
  • 23

2 Answers2

2

Using list comprehensions

>>> [l for l in list_dirs if l.split('\\')[-1] not in unwanted_files]
['C:\\bar\\foo\\.world.txt']

Use split to get filename

>>> [l.split('\\')[-1] for l in list_dirs]
['hello.txt', '.world.txt', 'yellow.txt']
styvane
  • 59,869
  • 19
  • 150
  • 156
  • Looking a little closer right now. Do this implementation required the correct filename of unwanted files or just a substring? I tested and it seems that required all the correct filename – dudas Feb 22 '15 at 11:25
  • Any chance we can modify this to be a substring of the correct filename? – dudas Feb 22 '15 at 11:36
  • `l.split('\\')[-1]` in my answer is the substring of the correct filename. see my edit – styvane Feb 22 '15 at 11:38
  • Sorry I probably explain me wrong. Imagine this case: 'world.txt' and 'word.txt~'. If I place 'wo' in the unwanted files, I would like to remove both – dudas Feb 22 '15 at 11:41
  • 1
    @dudas: If you need that full substring detection capability, you should say so in the question! To do that requires a different strategy than Michael9's solution: it needs a double loop. – PM 2Ring Feb 22 '15 at 11:57
  • @PM 2Ring That idea was born along with this discussion, that's why I didn't put that in first place. Im wondering if anyone can extend the implementation to the substring of the unwated_file names – dudas Feb 22 '15 at 12:00
  • 1
    @dudas: In that case, maybe you should ask a new question, with appropriate example input. – PM 2Ring Feb 22 '15 at 12:05
  • @dudas as PM 2Ring said you should ask another question – styvane Feb 22 '15 at 12:07
1

you also could use a filter function with lambda

print filter(lambda x: x.split('\\')[-1] not in unwanted_files, list_dirs)
#['C:\\bar\\foo\\.world.txt']

or if you don't mind to import os (imo this is cleaner then splitting the string)

print filter(lambda x: os.path.basename(x) not in unwanted_files, list_dirs)

In a list comprehension it would look like this

[l for l in list_dirs if os.path.basename(l) not in unwanted_files]
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51