मैं एक ही जरूरत में चल रही है और निम्नलिखित समारोह बनाने समाप्त हो गया:
def kwarg_map(element_constructor, **kwarg_lists):
"""
A helper function for when you want to construct a chain of objects with individual arguments for each one. Can
be easier to read than a list expansion.
:param element_constructor: A function of the form object = fcn(**kwargs)
:param kwarg_lists: A dict of lists, where the index identifies two which element its corresponding value will go.
:return: A list of objects.
e.g. Initializing a chain of layers:
layer_sizes = [784, 240, 240, 10]
layers = kwarg_map(
Layer,
n_in = layer_sizes[:-1],
n_out = layer_sizes[1:],
activation = ['tanh', 'tanh', 'softmax'],
)
is equivalent to:
layers = [Layer(n_in=784, n_out=240, activation='tanh'), Layer(n_in=240, n_out=240, activation='tanh'), Layer(n_in=240, n_out=10, activation='softmax')]
"""
all_lens = [len(v) for v in kwarg_lists.values()]
assert len(kwarg_lists)>0, "You need to specify at least list of arguments (otherwise you don't need this function)"
n_elements = all_lens[0]
assert all(n_elements == le for le in all_lens), 'Inconsistent lengths: %s' % (all_lens,)
return [element_constructor(**{k: v[i] for k, v in kwarg_lists.iteritems()}) for i in xrange(n_elements)]
उन दो बातें नहीं हैं बराबर। 'मानचित्र() 'फ़ंक्शन कॉल से लौटाए गए मानों की एक सूची (या 3.x, जेनरेटर में) बनाता है। यदि आप उन मानों का उपयोग नहीं करना चाहते हैं, तो 'मानचित्र()' का उपयोग न करें, केवल सामान्य पाश का उपयोग करें। –
'map' एक फ़ंक्शन है (जो एक सूची देता है), और लूप कोड वापसी के बिना कोड है। वे बराबर नहीं हैं। –