i=0 lst=d for i in range(dl): f=d[i] print f if fnmatch.fnmatch(f,mask): print "ff" else: print "file is not be reader" del lst[1] i=i+1 

The lst array duplicates the d array, after the array elements that do not match the mask should be removed, but instead the python is cried by IndexError: list index out of range

and here in this place

 f=d[i] 

Although when deleting this line, everything works

 del lst[1] 

But I need to remove unmatched elements, who knows what the problem is?

  • Yes, dl = len (d) - Hdd

3 answers 3

What is your "dl" equal to? If it is larger than the dimension d - then what is the question? operation:

 del lst[1] 

you remove an element not only from lst, but also from d. To make a copy of d in lst - use slice:

 lst = d[:] 

Well, it already depends on the logic of your script as a whole.

  • Thanks, it helped. - Hdd
  • @Hdd, If you are given an exhaustive answer, mark it as accepted. - Nicolas Chabanovsky

Python has an excellent "filter" function that returns a list of filtered objects.

In your case, you can write something like this:

 import functools filter_func = functools.partial(fnmatch.fnmatch, mask) filter(filter_func, lst) 

filter (filter_func, lst) returns an iterator over all elements of the lst list that match the mask.

    To start

     from copy import deepcopy lst = deepcopy(d) # вместо lst = d 

    Without this, lst and d will refer to the same object.

    • Thanks, helped - Hdd