0

Before, I asked that for search or filter values in two-segment list in Google Earth Engine at this link(Search or filter values in two-segment list in Google Earth Engine) and this question is answered by Daniel Wiell with very good explanation (https://gis.stackexchange.com/a/392774/112517).

I want to search for value in first column of the list, get finding result from second column and save to result to another new list. I want to search value from first column of lists and get second column value as result, for example, I want to search 101 and I get 12155 and save 12155 to new list.

var list = ee.List([
  [101, 12155],
  [123, 12333],
  [102, 12157],
  [102, 12222],
  [102, 12244],
  [103, 13333],
  [103, 12158], 
  ]) // This list contains thousands of paired values

If this list have repeated values such(102), I don't know how to achieve other paired values(12222, 12244). For example, if I search for 102, I get only 12157 as result.

How can I achieve other paired values(12222, 12244) and append results to new list?

1
  • 1
    I'm just wondering, what is the specific use case you have on Earth Engine with this list querying?
    – JonasV
    Apr 21, 2021 at 9:38

1 Answer 1

2

You can try following script. It finds indices of repeated values (in this case 102) in column 0 of list variable. Finally, these indices are used for printing repeated paired values in corresponding list.

var list = ee.List([
      [101, 12155],
      [123, 12333],
      [102, 12157],
      [102, 12222],
      [102, 12244],
      [103, 13333],
      [103, 12158], 
      ]); // This list contains thousands of paired values

print("original list", list);

var Column = 0;

var keyValues = list.map(function(inner) {
    return ee.List(inner).get(Column);
});

print("selecting column where 102 is", keyValues);

var num = ee.Number(102);

var seq = ee.List.sequence(0, keyValues.size().subtract(1));

var indexRep = seq.map(function (e) {

  var list = keyValues.map(function (ele) {
  
    return ee.Algorithms.If(ee.Number(ele).eq(num), ele, 0);
  
  });

  return ee.Algorithms.If(ee.Number(ee.List(list).get(e)).neq(0), e, -1);

}).removeAll([-1]);

print("index of 102 values", indexRep);

var pairedRep = indexRep.map(function (ele) {
  
  return list.get(ele);
  
});

print("paired values with 102", pairedRep);

Result of running above script in GEE console editor is as follows:

paired values with 102
[[102,12157],[102,12222],[102,12244]]

Editing Note:

Modified above script for printing only values (102 as test key value):

var list = ee.List([
  [101, 12155],
  [123, 12333],
  [102, 12157],
  [102, 12222],
  [102, 12244],
  [103, 13333],
  [103, 12158], 
  ]); // This list contains thousands of paired values

print("original list", list);

var Column = 0;

var keyValues = list.map(function(inner) {
    return ee.List(inner).get(Column);
});

print("selecting column where 102 is", keyValues);

var num = ee.Number(102);

var seq = ee.List.sequence(0, keyValues.size().subtract(1));

var indexRep = seq.map(function (e) {

  var list = keyValues.map(function (ele) {
  
    return ee.Algorithms.If(ee.Number(ele).eq(num), ele, 0);
  
  });

  return ee.Algorithms.If(ee.Number(ee.List(list).get(e)).neq(0), e, -1);

}).removeAll([-1]);

print("index of 102 values", indexRep);

var valuesPairedRep = indexRep.map(function (ele) {
  
  return ee.List(list.get(ele)).get(1);
  
});

print("values for repeated 102", valuesPairedRep);

Modified script by using 'iterate' method (101 as test key value):

var list = ee.List([
  [101, 12155],
  [123, 12333],
  [102, 12157],
  [102, 12222],
  [102, 12244],
  [103, 13333],
  [103, 12158], 
  ]); // This list contains thousands of paired values

print("original list", list);

var Column = 0;

var keyValues = list.map(function(inner) {
    return ee.List(inner).get(Column);
});

print("selecting column where 101 is", keyValues);

var num = ee.Number(101);

var new_seq = ee.List.sequence(0, 6);

var first = new_seq;

var appendValues = function(iter, previous) {
  
  return ee.List(previous)
    .set(iter, ee.Algorithms
    .If(ee.Number(ee.List(list.get(iter)).get(0)).eq(num), iter, -1));

};
  
var indexRep = ee.List(new_seq.iterate(appendValues, first))
  .removeAll([-1]);

print("indices for 101", indexRep);

var valuesPairedRep = indexRep.map(function (ele) {
  
  return ee.List(list.get(ele)).get(1);
  
});

print("values for 101", valuesPairedRep);
5
  • Thanks Xunilk, but it does not work for 101, and i only need the result value. I have thousands of paired values like above list, i must search value from first column and get result from second column. It must be iterate for all list values. Apr 21, 2021 at 16:18
  • 1
    I fixed the first issue. I forgot that the list also has index 0. I will see other issues later because I have a shorter solution with iterate.
    – xunilk
    Apr 21, 2021 at 19:30
  • 1
    I put an Editing Note in my answer with another script by using 'iterate' list method.
    – xunilk
    Apr 21, 2021 at 21:45
  • Thanks Xunilk, this code works for one value. But, i can not complete this code to search all of the first values, find second column value and add all of them to a new list. Apr 23, 2021 at 18:48
  • Dear Xunilk, how can i iterate all of first ID s and save secondary values to a new list? Your code run only for one search and save it to a list May 14, 2021 at 7:46

Your Answer

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

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