'How to use a command all the 4th item nested lists in netlogo

I have built a long nested list having the following profile: set my-list [A 1 2 3 4] [B 5 6 7 8] [C 9 10 11 12]

I'd like to apply the meancommand to the fourth item of each nested lists, so in the example to 4 8 12

but without building a list in loop that would look like [4 8 12] (to save computing time).

Is it possible ?

using let comp mean (item i item 4 (my-list)) or let comp mean (item 4 (my-list)) aren't obviously working.

The answer would be useful to other part of the model that I'm building.

Thanks for your time.



Solution 1:[1]

First things first: did you mean to say that such nested list is built such as the one below?

set my-list [[A 1 2 3 4] [B 5 6 7 8] [C 9 10 11 12]]

Note the extra pair of square brackets, that make this actually a list of lists. Even if that was the case, NetLogo wouldn't let you use that syntax:

  • Either because A, B and C are lacking quotation marks if you intend them to be strings;
  • Or because, if A, B and C are variables, NetLogo expects literal values (we can get around this problem by using (list ...) instead of []).

In the first case, it would have to be:

set my-list [["a" 1 2 3 4] ["b" 5 6 7 8] ["c" 9 10 11 12]]

In the second case, it would have to be:

set my-list (list (list a 1 2 3 4) (list b 5 6 7 8) (list c 9 10 11 12))

All of the above just to make sure we are all on the same page (in general, please make sure that the code you post in your question is valid for the language you are asking about. As you can see, it would save a lot of time and space!).

Anyway I imagine that what you come up with is something of this type:

[["a" 1 2 3 4] ["b" 5 6 7 8] ["c" 9 10 11 12]]

I would use a while loop to iterate through the inner lists. You can create a local variable to keep track of the sum of the numbers you extract as you iterate through the inner lists, and then divide that sum by the number of times you extracted a number:

to check-mean
  let my-list [["a" 1 2 3 4] ["b" 5 6 7 8] ["c" 9 10 11 12]]
  
  let the-sum 0
  
  let i 0
  while [i < length my-list] [
    set the-sum (the-sum + item 4 (item i my-list))
    set i i + 1
  ]
  
  print the-sum / i
end

Solution 2:[2]

From the answers above and adding a step in the procedure (mean and sd for groups of items having the same "region" in the list), here-below is my final code using map as the mean and sd are already calculated in a while-loop. Moreover, I assumed that calculating manually the standard deviation would create even more lists of list and complicate the code.

to create-successor-list
   
  set successor-list map [inner-list -> (list inner-list 0 0 ) ] region-data
  let i 0
  while [i < length region-data] [
    let current-item item i region-data
      set temp-profitability-list (filter [current-inner-list -> (item 1 current-inner-list = current-item)] profitability-list )
      set prof-mean mean map [x -> item 4 x ] temp-profitability-list
      set prof-sd standard-deviation map  [x -> item 4 x ] temp-profitability-list
      set successor-list replace-item i successor-list (replace-item 1 item i successor-list (prof-mean))
      set successor-list replace-item i successor-list (replace-item 2 item i successor-list (prof-sd))
      set i i + 1
      set temp-profitability-list [ ]
  ]    
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matteo
Solution 2 Sarahdata