This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: non-sequence map() arguments for optimization
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ecnassianer, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2005-09-02 23:30 by ecnassianer, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg54608 - (view) Author: Ecnassianer of the Green Storm (ecnassianer) Date: 2005-09-02 23:30
I'm trying to optimize some code thats similiar to this:

anObject = someConstructor() # This isn't a sequence
myList = myListMaker()  # This IS a sequence

for items in myList:
     function(items, anObject)


I'd like to be able to do this:
map(function, myList, anObject)

But the map function takes two lists, so I have to do
somethign like this:

list2 = []
for x in range(len(myList)):
     list2.append(anObject)
map(function, myList, list2)

But intializing list2 kind of defeats the purpose of
optimization. I was looking for some way to convince
anObject that it was really a list containing a lots of
entries of itself, but couldn't find anything in the API.

What I'd love to have is a version of the map function
which takes a function, a sequence, and an object as
arguments and calls the function with the first
argument as an element in the sequence, and the second
element as the object for as many times as their are
items in the sequence. 

(Note: Currently the API says if you pass sequences of
unequal lenghths to map, it uses None to fill up the
rest of the calls)
msg54609 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-09-03 06:43
Logged In: YES 
user_id=1188172

Use a list comprehension:

[function(item, anObject) for item in myList]

or, if you must use map,

map(lambda item: function(item, anObject), myList)

And please direct such questions to comp.lang.python in the
future.
msg54610 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-09-03 16:26
Logged In: YES 
user_id=80475

from itertools import imap, repeat

list(imap(function, myList, repeat(anObject)))
msg54611 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-09-03 16:36
Logged In: YES 
user_id=80475

Or just:

map(function, myList, repeat(anObject, len(myList)))
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42335
2005-09-02 23:30:05ecnassianercreate