2

In this very helpfull code from Noel Gorelick, it makes a nested-link from two-segment list. But if this list has repeated values, it does not give correct values.

var list = ee.List([
[99, 101],
[101, 103],
[102, 103],
[103, 109],
[109, 122],
[117, 156],
[128, 133],
[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, take this value as first column value and search again.

For example, suppose that i want to search for value "101" from first column of second row from the list with this code:

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

var start = [ee.List(list.get(1)).get(0)] 
print (start)  //  101

// 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) 

This code runs and give result as: [101,103,109,122,133]

But there are repeated values in this list, this list must be [101,102,103,109,122,128,133]

Please, how can get this list with repeated values?

3
  • I'm trying to understand your problem. The algorithm you are using is designed to traverse links such that ColumnA[X] -> ColumnB[X] (and vice-versa) is a valid step. The output you describe has invalid links (eg: 101 -> 102), which is why it doesn't appear in the output. Can you elaborate on what the algorithm is seeking to find? Commented Jul 7, 2021 at 22:28
  • it is like a tree branches, when i search a branche, i want to get all connected branches Commented Jul 9, 2021 at 5:49
  • In your desired output there is no way to distinguish branch stubs from the main path. Is that okay? Commented Jul 9, 2021 at 22:27

0

Your Answer

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

Browse other questions tagged or ask your own question.