In earthengine-api (Python),I use the the function called .map()
,for example:foo = col.map(lambda arg: arg)
,However ,I want to do some complex mapping operations,According to the official tutorial,Anonymous function mapping is achieved via lambda functions, which accept only a single expression. Use traditional named functions when complex mapping operations are required.
so i write the code:
import ee
import geemap
ee.Initialize()
list1=[]
def function1(arg):
list1.append("a")
list1.append("b")
return ee.Number(arg).add(1)
serverList = ee.List.sequence(0, 7)
a = serverList.map(lambda arg: function1(arg))
print(a.getInfo())
print(list1)
I want to use the map to do more things,while the element in serverList can add 1,the list1 can append two chars or do some oter things,but the I can not understand the result of the code:
[1, 2, 3, 4, 5, 6, 7, 8]
['a', 'b', 'a', 'b', 'a', 'b']
although each element in severList pluses 1,Only three groups of "a" and "b" have been appended in list1.I expected to add eight groups of 'a' and 'b' in list1.someone points me that i can not use the lambda expression in this way.Anonymous function mapping is achieved via lambda functions, which accept only a single expression. Use traditional named functions when complex mapping operations are required. So,How can I use the traditional named functions?
If i wirte the code,by this way:
import ee
import geemap
import os
ee.Initialize()
list1=[]
def function1(arg):
list1.append("a")
list1.append("b")
return ee.Number(arg).add(1)
serverList = ee.List.sequence(0, 7)
a = serverList.map(function1(arg))
print(a.getInfo())
print(list1)
There will be an error that "arg" is not defined,so how can use traditional named functions when complex mapping operations are required.
someone also points me that I confuse the functions in Client,Server,and python built-in syntax,But my real goal is to simplify the code,I want to use the .map()
funtion to do more things.Because According to theo fficial tutorial:
Use map()
to iterate over items in a collection. (For loops are NOT the right way to do that in Earth Engine and should be avoided).In python,I can do lots of things in a code block of For loops
,so I noly want to use .map()
to do more thing
How can I solve the problem?