1

Now, after Noel Gorelick's very helpfull code, i have a sorted list with two-segment:

var list = ee.List([
  [101, 103],
  [102, 103],
  [103, 109],
  [109, 122],
  [117, 156],
  [122, 133],
  [133, 100],
  [199, 155] 
  ])

i want to make a new list with linked-lists, for example i want to find an element from first column, take second column value and add the value to list, until the second value is equal to 100.

The result list have to a list with one column and values in this list must be:

var newlist = ee.List([101,102,103,109,122,133])

Any suggestion please?

3

This is not a good plan, but you can make this work with list.iterate, at least until your list gets big.

var list = ee.List([
  [101, 103],
  [102, 188],
  [103, 109],
  [109, 122],
  [117, 156],
  [122, 133],
  [133, 100],
  [199, 155] 
  ])


// Make a dictionary so lookups aren't O(N).  Keys must be strings.
var dict = ee.Dictionary(list.map(function(inner) {
  inner = ee.List(inner)
  return [inner.getNumber(0).format("%d"), inner.getNumber(1)]
}).flatten())
print(dict)

// Fill in the first value to get things started.
var start = [ee.List(list.get(0)).get(0)]

// Can only iterate a fixed number of times.  Use the length of the list.
var final = ee.List.sequence(0, list.size()).iterate(function(unused, result) {
  result = ee.List(result)

  // Next node number is the last one in the results list.
  var value = dict.getNumber(result.get(-1));
  
  // If the lookup value is 100, do nothing, else append it to the list.
  return ee.Algorithms.If(value.eq(100), result, result.add(value))
}, start)

print(final)
3
  • 2
    This question is a little strange for Earth Engine. What bigger picture problem are you trying to solve with this? Apr 5 at 14:36
  • This code is working, Thank you Noel, but, can we make a solution without using a dictionary? Can we make a function to get first column value from list and iterate with second column value? My goal is, make a function with start value and list name parameters and returns a sorted-connected list. And you asked what a bigger picture problem is, please, how can mail to you to explain my project details?
    – Mehmet C
    Apr 7 at 5:30
  • Dear Noel, i noticed that, when i have two rows with same second values, this code get only one row, how can we iterate for other rows, please? The list must have : 101, 102, 103,109,122,133
    – Mehmet C
    Jun 16 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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