Wednesday 11 November 2020

Python - Filter nested inside of map produces unexpected output

I have a list of filenames (strings) and and a set ls consisting of floats. Initially I want to filter all files that match each element of ls according to a predetemined expression: I convert all floats that are actually integers to integers and feed that into .format to create an appropriate search string (exprs). This produces the expected sequence of strings. I now want to filter 'files' using re.search, but as I understand it I need a different filter for each output of exprs. So I nested this inside of a map function:

t = 'Matrix'
exprs = map('{}_spike_{}_D1_1'.format , cycle([t]) ,(int(x) if x.is_integer() else x for x in ls))
y = map(lambda f:filter(lambda i : re.search(f,i), files), exprs)

Print(next(exprs)) produces the expected output i.e. 'Matrix_spike_50_D1_1'. If i 'freeze' the expression in re.search i.e. by doing b = next(exprs) and re.search(b, [...]) I get the expected output (i.e. the filename, correctly selected). But when I try to use map to consume all outputs of exprs and return resulting filter([...])'s I get

  1. a filter object instead of a map object
  2. two identical filter objects ,when running it exchaustively by means of a while True, catching all StopIterations and resuming

How can I modify this to return files that filter returns for each exprs?



from Python - Filter nested inside of map produces unexpected output

No comments:

Post a Comment