2

I am able to convert a list of numbers to a list of strings in the code editor following the answer provided here (more or less):

var numbers = ee.List.sequence(0, 4);
print(numbers);
var number_to_string = function(number) {
  return ee.String(ee.Number(number).int());
};
var strings = numbers.map(number_to_string);
print(strings);

[0,1,2,3,4]

["0","1","2","3","4"] <- strings as expected

However, when I try to run the same code (reformatted) in the Python API, the output list still contains numbers, not strings:

numbers = ee.List.sequence(0, 4);
print(numbers.getInfo());
def number_to_string(number):
    return ee.String(ee.Number(number).int());
strings = numbers.map(number_to_string);
print(strings.getInfo());

[0, 1, 2, 3, 4]

[0, 1, 2, 3, 4] <- Why aren't these strings?

How can I force the conversion of a list of numbers to a list of strings in the Python API?

1 Answer 1

1

In general, you can't just cast from one type to another like that, unless the types are part of the same type hierarchy (ie: Images are also Features)

Use ee.Number(number).format("%d")

The JavaScript API might have gotten updated to look for numbers in the String constructor. It looks like the Python API didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.