When running the following code, I get an error as follows:
for (x in List[0] and y in range(0,11)):
^
SyntaxError: invalid syntax
I am very new to Python programming. When I try to run the iteration at the top I get the above error-message.
Could someone please kindly explain which part of the syntax is invalid?
The tiny arrow underneath seems to point to the colon.
Many thanks.
Syntax Error: The brackets are not necessary; use the following:
for x in List[0] and y in range(0, 11): ...
More Problems Although as in my comment, this won't necessarily work.
Merging the Arrays:
If you are trying to go through all values in List[0] and then in range(0, 11), do the following:
for x in (List[0] + range(0, 11)): ...
No Duplicates:
Or, if you want no duplicates, and ordering does not matter, use this:
for x in list(set(List[0] + range(0, 11))): ...
Going through all pairs:
Or, if you are trying to go through all pairs consisting of a value in List[0] and a value in range(0, 11), use the following:
for x in List[0]:
for y in range(0, 11):
...
Related
How can I translate the following code of R to Julia? I am new in Julia.
I know Julia has different ways to replace for loop.
max_trial <- max(dataframe[,1])
max_c1 <- NA
for(c in 1:max_trial){
c1 <- which(dataframe[,1]==c)
max_measure <- max(dataframe[c1,2])
max_c1[c] <- max_measure
}
As suggested I applied the following code translate
max_c1= []
for c in 1:max_trial
c1 = findall(dataframe[:,1] .== c)
max_c1[c] = maximum(dataframe[c1,2])
end
But I received the following error
ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index [1]
Also the values received from this translation “ maximum(dataframe[c1,2])” is still different than The R code. It seems for this part of error some adjustment of the syntax needs improvement.
I think the corresponding Julia code would look like
for c in 1:max_trial
c1 = findall(dataframe[:,1] .== c)
max_c1[c] = maximum(dataframe[c1,2])
end
although I think you did not give enough information to completely answer your question, so I'm not really sure. Maybe adding the data you used and the output you are looking for in your question would help?
The idea is as follows: Suppose I have a list P = [(1,0),(4,3)] or similar. I want to evaluate the polynomial that's defined by this list in the manner: 1X^0 + 4X^3.
To do this, I've written the following:
evaluate(P,X,Y) :- evaluate(P,X,Y,0).
evaluate([],_,S,S).
evaluate([P1,P2|Ps],X,Y,S) :-
S1 is S+P1*X^P2,
evaluate(Ps,X,Y,S1).
Which is supposed to succeed when Y is the sum of the polynomial P, given x=X.
The problem is that when I try and run this code, I get the error:
is/2: Arithmetic: `(',')/2' is not a function
But I have no idea where this is coming from or how to fix it.
I did try splitting the S1 is up in to its segments, but doing that didn't help.
EDIT: Ok, I found out that it's about the way the list is written down. How do I work with tuples in this way within the bounds of Prolog?
Your problem is that your data structure for each item in the list is a tuple as you noted and where you access the values of tuple in the list is not correct.
This
evaluate([P1,P2|Ps],X,Y,S) :-
should be
evaluate([(P1,P2)|Ps],X,Y,S) :-
Notice the parenthesis around P1,P2.
When I run with the change I get
?- evaluate([(1,0),(4,3)],5,Y).
Y = 501.
Also it is common to put the output arguments at the end,
evaluate_01(P,X,Y,0).
as
evaluate_01(P,X,0,Y).
and then change the other predicates as necessary.
evaluate_02(P,X,Y) :- evaluate_02(P,X,0,Y).
evaluate_02([],_,S,S).
evaluate_02([(P1,P2)|Ps],X,S,Y) :-
S1 is S+P1*X^P2,
evaluate_02(Ps,X,S1,Y).
As an interesting option, this can be done with maplist/3 and sumlist/2:
evaluate_poly(Poly, X, R) :-
maplist(evaluate_term(X), Poly, EvaluatedTerms),
sumlist(EvaluatedTerms, R).
evaluate_term(X, (Coeff, Power), TermValue) :-
TermValue is Coeff * (X ^ Power).
I'm trying to make a recursive function with Ocaml, but I keep getting the same error code.
let rec get x =
if x > 7 then
get x-7;;
And I get the very useful error message of:
Error: This expression has type int but an expression was expected of
type unit
I'm a complete beginner at OCaml, and studying it for a module at university.
And this is one of my assignments, and I'm a bit stuck!
I originally wanted to do it by a while loop, (as I'm a predominantly imperative programmer), but I couldn't get that to work, so I thought I'd try recursive!
Thanks
There's two problems with this code. First, the spacing of x-7 indicates that you would like to pass x - 7 to get, but it will actually be parsed as (get x) - 7. That's easily fixed with parentheses:
let rec get x =
if x > 7 then get (x - 7)
The second problem is that you don't have a second arm for the if, so the function doesn't have much of a chance of returning anything. (One arm if is taken to be of type unit, only useful for effects.)
You probably want to return something if x is less than 7, maybe:
let rec get x =
if x > 7 then get (x - 7) else x
Writing this with a while loop is possible, but you should understand that variables in OCaml are not mutable locations, only names. You'll have to introduce and manipulate mutable places explicitly:
let get x =
let y = ref x in
while !y > 7 do
y := !y - 7;
done;
!y
Hope that helps.
What I want to do is simple:
collection = {'a':[], 'b':[], 'c':[]}
values = [1,2,3]
I want to make a function that produces the following: (append the values into the list element of the dictionary, the dic and the list are the same length)
{'a':[1], 'b':[2], 'c':[3]}
This is simple enough and I can do it using, a couple of for x in. But I want to do this in one line. (using two loops in the same line) and I can not get the syntax to work.
I have tried some things similar to this, but they all result in syntax error:
collection[c].append(value), for c in d.iteritems(), for value in values
You can't do what you want to do on one line. You can create a new dictionary on one line though:
{k: collection[k] + [v] for k, v in zip(collection.keys(), values)}
Result:
>>> collection = {'a':[], 'b':[], 'c':[]}
>>> values = [1,2,3]
>>> {k: collection[k] + [v] for k, v in zip(collection.keys(), values)}
{'a': [1], 'c': [2], 'b': [3]}
This is called a dict comprehension. Like a list comprehension and a generator expression, you can use multiple loops in one of those, but you don't need one here. zip() will pair up the keys from collection with the integers from values.
To modify a dict in-place, you'll have to use 2 lines at least:
for k, v in zip(collection.keys(), values):
collection[k].append(v)
Python does accept that on one line, but that goes against just about every styleguide I can look up for you:
for k, v in zip(collection.keys(), values): collection[k].append(v)
Python throws a syntax error because it interprets your line as a tuple of expressions (the commas make it a tuple), and two of your expressions are for statements, which cannot be used in an expression.
A simple question, how would I go about declaring a clause which would produce the number specified +1 +2 and +3? I have tried:
addup(Thenumber,Thenumber+1).
addup(Thenumber,Thenumber+2).
addup(Thenumber,Thenumber+3).
but when I run it with say, Thenumber=5, it just returns 5+1 5+2 5+3. I have tried using 'is' to force it to evaluate but it doesn't seem to work. Any help would be appreciated.
Try this:
addup(X, Y) :- Y is X + 1.
or
addup(X, X+1).
and you question should be addup(2, X)
then X should be 3. If you want to parametrize your addup parameter just make it:
addup(X, Y, X + Y).
and ask with addup(5, 6, X).