Python lists and iterables
in Python 3.2 How do I alter items in a list with a function and return the result of the function over the item and the item itsel开发者_如何转开发f before altering? 10x
def some(func,seq):
# What to do here?
Something like
def func(my_func,seq):
return seq, [my_func(n) for n in seq]
or
def func(my_func,seq):
return [(my_func(n),n) for n in seq]
... your description is not very clear in that regard.
What's being used here is a list comprehension. my_func
is called once for every element in seq
and should return the function value for this element.
精彩评论