How to add a static list to a list? - list

When adding a global variable to a list, does vim add this variable as a dynamic list?
input:
g:ListTotal = []
let g:mylist = ['hi','2','','']
call add(g:ListTotal, g:mylist)
echo g:ListTotal --> ['hi','2','',''] => ok
Then in a script g:mylist is changed p.e.
let g:mylist[0] = 'hello'
echo g:mylist --> = ['hello','2','',''] => ok
adding again this list to g:ListTotal:
call add(g:ListTotal, g:mylist)
:echo g:ListTotal -->
Output::
[['hello','2','',''],['hello','2','','']]
Expected output:
[['hi','2','',''],['hello','2','','']]
Does vim dynamically update lists when they're added to another list?
How can I add a list statically to another list?

I believe list variables are just pointers to the list so adding to list just add that pointer which is why changing looks like it changes both.
If you want a unique list you can copy the list.
call add(g:ListTotal, copy(g:mylist))
Or
call add(g:ListTotal, deepcopy(g:my list))
Read :h copy() and :h deepcopy().

Related

How to remove a "List of items" from a list?

I have two lists : _bufferCarnetList and _deletedWords.
When the user hits OK: I want to remove from _bufferCarnetList the items contained in _deletedWords.
I have tried using the .remove method, but nothing happens.
_bufferCarnetList.remove(_deletedWords).
Is there a way to do it in a direct way, or should I iterate the list and remove each item separately ?
Use the removeWhere method:
_bufferCarnetList.removeWhere((e) => _deletedWords.contains(e));

Netlogo: delete list content

How do I delete the content of a list in Netlogo?
This is a tuned-down version of my code to function as an example:
to calculate_SN
ask turtles [
set subjective_norm_list []
set subjective_norm_list [1 2 3 4 5]
set subjective_norm ( sum subjective_norm_list / length subjective_norm_list)
*delete content of subjective_norm_list so that it is empty again*
end
The part between asterisks I don't know.
Based on your shared code so far, you should take a different approach: create a function.
to-report subjective-norm [#lst]
report (sum #lst) / (length #lst)
end
It is unclear that you will ever need to assign a variable name to your list. You may be able to use it upon creation and then forget about it. (It will be garbage collected.)
If you want subjective_norm_list to be an empty list, you can set it to an empty list, just like you did when you initialized it the first time around:
set subjective_norm_list []
Note that, technically, NetLogo lists are immutable, so you're not deleting the elements in the list: you're just creating a new list with no elements in it and assigning it to the same variable. But for all intents and purposes, it's the same: subjective_norm_list is empty again.

Erlang: Printing out specific value in list

If I have a list for example:
[{list1, [1,2]},{list2, [3,4]}]
How would I print out [3,4] using io:format if this is passed in as a variable, for example I.
I'm currently doing:
io:format("list 2: ~w~n", [I]),
Your example list is in the form: [{Key1, Value1}, {Key2, Value2}, ...], where Key is an atom. This kind of list can also be called a proplist (property list). The module named proplist, can handle exactly this datastructure.
In your case, you could just run:
PList = [{list1, [1,2]},{list2, [3,4]}],
Value = proplists:get_value(list2, PList),
io:format("list2: ~p~n", [Value]).
The variable Value is now bound to the value [3,4].
See also: The Erlang-Documentation page for proplists

difference between [] and list() in python3

I thought that [] and list() were two equal ways to create a list. But if you want a list with dictionnary keys,
var = [a_dict.keys()]
doesn't work since type(var) is [dict_keys], correct syntax is :
var = list(a_dict.keys())
I couldn't find an good explanation on this behaviour. Do you have one ?
TL;DR:
list() is the same as []
list(obj) is not the same as [obj]
a_dict.keys() is a dictionary view object, it returns an object which can be iterated to yield the keys of a_dict. So this line:
[a_dict.keys()]
is saying in python "I'm making a list with one element in it" and that one element is the dict keys iterator. It's a list literal in the syntax.
Now this line:
list(a_dict.keys())
is a call to the list builtin function. This function list attempts to iterate the argument and produce a list. It's a function call in the grammar.
The equivalent list literal (actually list comprehension) would be instead:
[key for key in a_dict.keys()]
Finally, note that dictionary objects iterate by keys anyway,
list(a_dict.keys()) would usually be written more simply as as list(a_dict) instead.
Hope this helps.
[a_dict.keys()]
This one puts a single element in the list. Just as if you were to write [1]. In this case that one element is going to be a list.
list(a_dict.keys())
The constructor accepts a sequence and will add all elements of the sequence to the container.

How to check and change the value of a list?

I would like to check all list values in a list and change them if necessary.
p.e.
I want to check the next lists if there are values higher or lower then the next values:
min-value = 6
max-value = 22
mylist = ['4-8','25','16-19','21-32']
if one of the list values is below the min-value or higher then the max-value, the list values must be changed to the min-value and max-value. p.e. in example, the new list must be:
mylist = ['6-8','22','16-19','21-22']
if the entire value of the list item is below the min-value or higher then the max-value the list item can be removed.
How can I check my list values and change them?
There are two approaches. In the procedural one, you iterate over the list items and modify or skip the element:
let newlist = []
for element in mylist
" Parse element.
if ! OutsideBounds(element)
call add(newlist, AdjustBounds(element))
endif
endfor
In the functional programming approach, you use the built-in map() to modify elements (i.e. adjust the bounds), but that one cannot remove elements. So just empty those elements and then do a second pass with filter() to remove them. Note that both functions modify the original lists, so use copy() if you need to keep the original.
call filter(map(mylist, 'AdjustBounds(v:val)'), '! OutsideBounds(v:val)')
I hope I don't need to tell you how to write the AdjustBounds() and OutsideBounds() functions...