Let's say I get a list, and I want to iterate on three at a time. I.e.: I have a list with [1,4,5,6,7,8,-9,2,0] In TCL, I can just use (for example):
foreach { x y z } $list {
puts "x is ${x}"
puts "y is ${y}"
puts "z is ${z}"
}
How can I define more than 1 variable, using the for loop with the in (array name) in Python 3.3? The Python org wiki showed just example of 1 iteration variable. This is also easily done in RubyThanks.
EDIT: The expected output is:
x is 1
y is 4
z is 5
x is 6
y is 7
z is 8
x is -9
y is 2
z is 0
You can split this array into chunks, and then just do something like this:
for x,y,z in chunked_array:
print("x=", x)
print("y=", y)
print("z=", z)
Related
I want to create a matrix with random numbers in J programming language when the required shape is derived from other variables.
I could create such a matrix with ? 3 5 $ 0 if i specify its shape using literal integers. But I am struggling to find a way to create such a matrix when the shape is # y and # x instead of 3 and 5 shown in above example.
I have tried ? 0 $~ # y, # x and it has not worked.
I think I need some way to apply # over a list of variables and return a list of numbers which should be placed after $~, somewhat like map functionality of other languages. Is there a way to do this?
I think that ?#:$ is what you are looking for
3 5 ?#:$ 0
0.031974 0.272734 0.792653 0.439747 0.136448
0.332198 0.00904103 0.7896 0.78304 0.682833
0.27289 0.855249 0.0922516 0.185466 0.257876
The general structure for this is x u#:v y <-> (u (x v y)) where u and v are the verbs and the arguments are x and y.
Hope this helps.
Rereading your question it looks as if you want the shape to be based on the number of items in the arguments. Here I would use # to count the items in each argument, then use , to create the left argument for $&0 and apply ? to the result.
3 4 5 (?#:($&0 #:,))&# 5 3 3 4 5
0.179395 0.456545 0.805514 0.471521 0.0967092
0.942029 0.30713 0.228288 0.693909 0.338689
0.632752 0.618275 0.100224 0.959804 0.517927
Is this closer to what you had in mind?
And as often the case, I thought of another approach overnight
3 4 5 ?#0:"0/ 1 2 3 4 5
0.271366 0.291846 0.0493541 0.72488 0.47988
0.50287 0.980205 0.58541 0.778901 0.0755205
0.0114588 0.523955 0.535905 0.5333 0.984908
I'm starting to wrap my head around Haskell and do some exciting experiments. And there's one thing I just seem to be unable to comprehend (previous "imperativist" experience talks maybe).
Recently, I was yearning to implement integer division function as if there where no multiply/divide operations. An immensely interesting brain-teaser which led to great confusion.
divide x y =
if x < y then 0
else 1 + divide (x - y) y
I compiled it and it.. works(!). That's mind-blowing. However, I was told, I was sure that variables are immutable in Haskell. How comes that with each recursive step variable x keeps it's value from previous step? Or is my glorious compiler lying to me? Why does it work at all?
Your x here doesn't change during one function call (i.e., after creation) - that's exactly what immutable means. What does change is value of x during multiple (recursive) calls. In a single stack frame (function call) the value of x is constant.
An example of execution of your code, for a simple case
call divide 8 3 -- (x = 8, y = 3), stack: divide 8 3
step 1: x < y ? NO
step 2: 1 + divide 5 3
call: divide 5 3 -- (x = 5, y = 3), stack: divide 8 3, divide 5 3
step 1: x < y ? NO
step 2: 1 + divide 2 3
call divide 2 3 -- (x = 2, y = 3), stack: divide 8 3, divide 5 3, divide 2 3
step 1: x < y ? YES
return: 0 -- unwinding bottom call
return 1 + 0 -- stack: divide 8 3, divide 5 3, unwinding middle call
return 1 + 1 + 0 -- stack: divide 8 3
I am aware that the above notation is not anyhow formalized, but I hope it helps to understand what recursion is about and that x might have different values in different calls, because it's simply a different instance of whole call, thus also different instance of x.
x is actually not a variable, but a parameter, and isn't that different from parameters in imperative languages.
Maybe it'd look more obvious with explicit return statements?
-- for illustrative purposes only, doesn't actually work
divide x y =
if x < y
then return 0
else return 1 + divide (x - y) y
You're not mutating x, just stacking up several function calls to calculate your desired result with the values they return.
Here's the same function in Python:
def divide(x, y):
if x < y:
return 0
else:
return 1 + divide(x - y, y)
Looks familiar, right? You can translate this to any language that allows recursion, and none of them would require you to mutate a variable.
Other than that, yes, your compiler is lying to you. Because you're not allowed to directly mutate values, the compiler can make a lot of extra assumptions based on your code, which helps translating it to efficient machine code, and at that level, there's no escaping mutability. The major benefit is that compilers are way less likely to introduce mutability-related bugs than us mortals.
I would like to count the number of occurrences of an event (for example, x data value equals some number) and store these occurrences in order, while plotting a file in gnuplot. Say I have the following file:
1
0
0
0
1
1
0
Now I want to count how many times I have a 1 and store that number in variable N. Then I want to know the positions where that happens and store that information in an array pos, all of this while plotting the file. The result, for the example above, should be:
print N
3
print pos
1 5 6
I know how to achieve the counting:
N = 0
plot "data" u ($0):($1 == 1 ? (N = N+1, $1) : $1)
print N
3
Then to achieve the position recording, it would be schematically something like this:
N = 0 ; pos = ""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = pos." ".$0, $1) : $1) # This doesn't work!
print N
3
print pos
1 5 6
How can this be done in gnuplot without resorting to external bash commands?
Well, as sometimes happens writing down the question triggers an idea for an answer. I'll leave it here in case somebody finds it useful:
N=0 ; pos=""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = sprintf("%s %g", pos, $0+1), $1) : $1)
print N
3
print pos
1 5 6
Note I had to use $0+1 because position 1 is treated by gnuplot as zero.
Let's say I have
a = [ 'i=' num2str(0)]
a =
i=0
and
A = zeros(2);
B = num2str(A)
B =
0 0
0 0
This i=0 is considered a 1x3 matrix: [ i, =, 0]. Now how do I transform this into one element so that I can replace B(1,1) with i=0? I want to get
B =
i=0 0
0 0
(This is the reason why I converted A into string.)
I kept getting this error:
Assignment has more non-singleton rhs dimensions than non-singleton
subscripts
which I suspect was due to a's dimension.
I've tried strcat(a), and some other methods.
Edit:
The motivation behind it is from my attempt to put in labels into a matrix while executing a loop.
This is the last portion of my code:
n5 = length(X(1, :));
n6 = length(X(:, 1)) + 1;
Y = zeros(n6, n5);
Y(2:n6, :) = X;
Z = num2str(Y, 4);
for i = 0:K
a = ['i=' num2str(i)];
Z(1,i+1) = a;
end
X = Z
end
I want the output to show:
i=0 i=1 ... i=K
1.0022 1.0000 ... 1.0000
2.0081 2.0000 ... 2.0000
4.0011 4.0000 ... 4.0000
3.9811 4.0000 ... 4.0000
I'm aware we can format the output in another way, but not in loops. I want to use loops.
Take 2:
I find it difficult to find a way to store in a matrix both strings (i=0...) as well as numbers. I would recommend the use of cell array
sz = size( X );
Z(2,:) = mat2cell( X, sz(1), ones(1,sz(2)) ); % convert each column of X into cell
Z(1,:) = arrayfun( #(x) sprintf('i=%d',x), 0:(sz(2)-1), 'uni', false );
The resulting cell array Z is of size 2x(n5) and would look like:
'i=0' 'i=1' 'i=2' 'i=3' 'i=4'
[5x1 double] [5x1 double] [5x1 double] [5x1 double] [5x1 double]
Where Z{2,ii} is the ii-th column of matrix X.
My book presents a simple example which I'm a bit confused about:
It says, "consider the following program, and assume that the fine-grained atomic actions are reading and writing the variables:"
int y = 0, z = 0;
co x = y+z; // y=1; z=2; oc;
"If x = y + z is implemented by loading a register with y and then adding z to it, the final value of x can be 0,1,2, or 3. "
2? How does 2 work?
Note: co starts a concurrent process and // denote parallel-running statements
In your program there are two parallel sequences:
Sequence 1: x = y+z;
Sequence 2: y=1; z=2;
The operations of sequence 1 are:
y Copy the value of y into a register.
+ z Add the value of z to the value in the register.
x = Copy the value of the register into x.
The operations of sequence 2 are:
y=1; Set the value of y to 1.
z=2; Set the value of z to 2.
These two sequences are running at the same time, though the steps within a sequence must occur in order. Therefore, you can get an x value of '2' in the following sequence:
y=0
z=0
y Copy the value of y into a register. (register value is now '0')
y=1; Set the value of y to 1. (has no effect on the result, we've already copied y to the register)
z=2; Set the value of z to 2.
+ z Add the value of z to the value in the register. (register value is now '2')
x = Copy the value of the register into x. (the value of x is now '2')
Since they are assumed to run in parallel, I think an even simpler case could be y=0, z=2 when the assignment x = y + z occurs.