list-comprehension and map question (simple)

Charles Hartman charles.hartman at conncoll.edu
Sun Mar 27 20:35:29 CEST 2005


On Mar 27, 2005, at 1:18 PM, Brian van den Broek wrote:

> >>> def some_arbitrary_function(y):
> ... 	return ( (y * 42) - 19 ) % 12
> ...
> >>> [some_arbitrary_function(len(x)) for x in lines.split()]
> [5, 5, 11, 11, 5, 11, 5, 11]
> >>>
>
>
> I could be missing some edge cases, but it seems to me that if you 
> have list comps you don't really need map, filter, and the like. The 
> map portion can be done by nested function applications in the list 
> comp itself.

A good point, and I think I see that. But ultimately what I'm wondering 
is whether a construction like this [1]:

         for s in possScansions:
             for a in algorithms:
                 (feet, test) = self.DoAlgorithm(a, s)
                 complexities[(s, a)] = (self._measureComplexity(feet, 
test), len(feet))

can be condensed in one or more of these ways. (Whether the result 
would be readable / maintainable is a separate question. So is whether 
it would be more efficient. At the moment I'm just trying to get clear 
about the syntax.)

[1] possScansions is a list of strings; algorithms is a list of ints; 
feet is a list of strings; test is a list of Booleans. complexities is 
a dictionary whose keys are those two-item tuples and whose values are 
the integers returned by self._measureComplexity

Charles Hartman




More information about the Python-list mailing list