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
Related
Having 2 lists, I want check which values of List1 are in List2. I'm trying as below but I get error
List1 = {3,2,8,7,5}
List2 = {1,3,4,2,6,7,9}
= List.Transform(List1, each Text.Contains(List2, _))
Expression.Error: We cannot convert a value of type List to type Text.
Details:
Value=[List]
Type=[Type]
My expected output would be 3,2,7.
How can I do this?
See List.Intersect Documentation
Intersect = List.Intersect({List1,List2})
#horseyride has probably the best answer but using your original logic, you could also write the intersection like this:
List.Select(List1, each List.Contains(List2, _))
This uses Select instead of Transform since you are trying to select/filter instead of changing the elements and uses the appropriate List type instead of Text for the Contains part.
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.
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.
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().
I want to append elements to a list and I'm not allowed to use the lists library or any other BIF. An example of how I want it to be:
Eshell V5.9.1 (abort with ˆ G)
1> Db = db:new().
[]
2> Db1 = db:write(apple, fruit, Db).
[{apple,fruit}]
3> Db2 = db:write(cucumber, vegetable, Db1).
[{apple,fruit},{cucumber,vegetable}]
The code I have now for this (not working):
write(Key, Element, []) -> [{Key, Element}|[]];
write(Key, Element, [H|T]) -> [H|write(Key,Element,T)].
The error I'm getting is when I do this:
3> Db2 = db:write(cucumber, vegetable, Db1).
** exception error: no match of right hand side value [{apple,fruit},{cucumber,vegetable}]
I understand the error message but I dont know how to go from here...
I suspect that this is just a case of Db2 already having a value, and it's a different value from the return value of db:write (which is [{apple,fruit},{cucumber,vegetable}] according to the error message). Type Db2. to see what value it has, and type f(Db2). to "forget" its value, so that you can assign to it again.
You can append element to the list by List ++ [Element]
Appending with ++ operator is not very recommended. You should use it only with small lists.
You have two options:
- lists:append (you said it's not an option ) or
- you can put in in front of a list with | operator.