I'm working with Map() and need an efficient method to loop through all the keys.
Specifically the keys are non matrices, and the image is a t_List of real vectors.
My current method is to turn the Map into a matrix and loop through like below
M = Map();
...\\fill up the map with stuff
matM = Mat(M);
for(i=1, matsize(M)[1],
L = matM[i,2];
\\ proceed to do stuff with L
);
However my understanding is that matM will create a copy of the data inside M, which I'd like to avoid if possible. My only other thought is to create a supplementary list of the ideals as the Map is filled, and then to iterate through that.
Is there a better way to handle this?
You can loop the map using a foreach().
{
foreach(M, item,
my(key = item[1][1]);
my(value = item[1][2]);
print(Str(key, ": ", value));
);
}
It looks a little bit weird because the variable item contains a vector whose first position it's another vector with the key and the value.
If you're going to use it often you could define a function like this:
foreachMap(M, expr) = foreach(M, item, expr(item[1][1], item[1][2]));
lambda = (key, value) -> print(Str(key, ": ", value));
foreachMap(M, lambda);
I am trying to write a code which deletes all repeated elements in a Vector. How do I do this?
I already tried using unique and union but they both delete all the repeated items but 1. I want all to be deleted.
For example: let x = [1,2,3,4,1,6,2]. Using union or unique returns [1,2,3,4,6]. What I want as my result is [3,4,6].
There are lots of ways to go about this. One approach that is fairly straightforward and probably reasonably fast is to use countmap from StatsBase:
using StatsBase
function f1(x)
d = countmap(x)
return [ key for (key, val) in d if val == 1 ]
end
or as a one-liner:
[ key for (key, val) in countmap(x) if val == 1 ]
countmap creates a dictionary mapping each unique value from x to the number of times it occurs in x. The solution can then be easily found by extracting every key from the dictionary that maps to val of 1, ie all elements of x that occur precisely once.
It might be faster in some situations to use sort!(x) and then construct an index for the elements of the sorted x that only occur once, but this will be messier to code, and also the output will be in sorted order, which you may not want. The countmap method preserves the original ordering.
Using hash package in R I created a hast table with keys and values. I want to add new keys and values to the existing hashtable. Is there any way?
Suppose
ht <- hash(keys = letters, values = 1:26)
And I need to add new keys and values to ht.
Is there any way other than
for eg :
ht$zzz <- 45
The documentation for the hash package provides a number of syntax varieties for adding new elements to a hash:
h <- hash()
.set( h, keys=letters, values=1:26 )
.set( h, a="foo", b="bar", c="baz" )
.set( h, c( aa="foo", ab="bar", ac="baz" ) )
The first .set option would seem to be the best for bulk inserts of key value pairs. You would only need a pair of vectors, ordered in such a way that the key value representation is setup the way you want.
I currently have an RDD with an array that stores a key-value pair where the key is the 2D indices of the array and the value is the number at that spot. For example [((0,0),1),((0,1),2),((1,0),3),((1,1),4)]
I want to add up the values of each key with the surrounding values. In relation to my earlier example, I want to add up 1,2,3 and place it in the (0,0) key value spot. How would I do this?
I would suggest you do the following:
Define a function that, given a pair (i,j), returns a list with the pairs corresponding to the positions surrounding (i,j), plus the input pair (i,j). For instance, lets say the function is called surrounding_pairs(pair). Then:
surrounding_pairs((0,0)) = [ (0,0), (0,1), (1,0) ]
surrounding_pairs((2,3)) = [ (2,3), (2,2), (2,4), (1,3), (3,3) ]
Of course, you need to be careful and return only valid positions.
Use a flatMap on your RDD as follows:
MyRDD = MyRDD.flatMap(lambda (pos, v): [(p, v) for p in surrounding_pairs(pos)])
This will map your RDD from
[((0,0),1),((0,1),2),((1,0),3),((1,1),4)] to
[((0,0),1),((0,1),1),((1,0),1),
((0,1),2),((0,0),2),((1,1),2),
((1,0),3),((0,0),3),((1,1),3),
((1,1),4),((1,0),4),((0,1),4)]
This way, the value at each position will be "copied" to the neighbour positions.
Finally, just use a reduceByKey to add the corresponding values at each position:
from operator import add
MyRDD = MyRDD.reduceByKey(add)
I hope this makes sense.
In R, I'd like to build a key-value paired list from separate key and value columns. In python I would just do something like this:
d = {k:v for k,v in zip(keys, values)}
I want something similar in R that is equivalent to:
list('key1' = 'value1', 'key2' = 'value2', ...)
I've built this with a for-loop but was hoping there is a more elegant R way of doing this.
You can use split to get a list of key/value pair
split(values, keys)