I have this function:
def applychange(L):
result=3+2
L=[4,5,6]
return result
L = [1,2,3]
print(applychange(L))
print(L)
and the result is :
5
[1, 2, 3]
how to change the L variable to new value to have this result:
5
[4, 5, 6]
If your goal is to modify L as global variable you need to declare it as such.
For your code to run as intended, insert global L after the result assignment in your function.
Additionally, there is no need to pass L to the function in order to modify L.
def applychange():
result = 3 + 2
global L
L = [4, 5, 6]
return result
L = [1, 2, 3]
print(applychange())
print(L)
Your code written this way will result in the intended output:
5
[4, 5, 6]
The problem is that you overwrite the variable L, instead of modifying it. You can use
L[:] = [4,5,6]
instead of
L = [4,5,6]
Related
I need to remove all elements from set that satisfy the lambda function, is there a way to make it continue when the fisrt part of the statement returns error?
f = lambda x : x < "t" or x % 2 == 0
s = {"a", 2, "u", 4, 3}
def sort(fun, s):
l = []
for element in s:
try:
if (fun(element)) == True:
l.append(element)
except:
pass
for element in l:
s.remove(element)
my code returns:
s = {2, 3, 4, 'u'}
but I need it to return:
s = { 3, 'u'}
Maybe you can adjust a lambda function to check the type of the element.
Also, to remove values from set use set-comprehension:
f = lambda x: (isinstance(x, str) and x < "t") or (
isinstance(x, int) and x % 2 == 0
)
s = {"a", 2, "u", 4, 3}
s = {v for v in s if not f(v)}
print(s)
Prints:
{'u', 3}
I want to create the function using Polynomials package in Julia, while c is a Vector and n is the length of the Vector. I can make it in JuMP package but not in Polynomials.
Any idea how to create this function?
Just call Polynomial on this vector c.
julia> c = [1, 2, 3, 4]
4-element Vector{Int64}:
1
2
3
4
julia> p = Polynomial(c)
Polynomial(1 + 2*x + 3*x^2 + 4*x^3)
julia> c = [2, 4, -8, 0, 0, 1];
julia> p = Polynomial(c)
Polynomial(2 + 4*x - 8*x^2 + x^5)
julia> p(2) #evaluate the polynomial at x = 2
10
I have this function
where f has values (18 19 20 21 22), and I should compute the value of the function for each value of f and plot each value.
I try to make f as vector 18:22, but it gives result from 1 to 22. Same result when I use for loop. My code is below, how can I modify it to take the values within the range only?
clc
fc=20;
theta=80;
N=16;
f=18:22;
g_m(f)=(sin((N*pi/2).*sin(theta).*(f/fc-1)))./sqrt(N).*(sin(pi/2).*(f/fc-1));
g_p(f)=exp(1j*0.5*(N-1).*pi*sin(theta).*(f/fc-1));
gain(f)=g_m(f).*g_p(f);
figure(1);
plot(f,g_m(f));
I believe this should give you enough information:
f = 5:7;
g(f)= [2, 2, 5]
g =
0 0 0 0 2 2 5
I can't seem to be able to create a function that takes two lists as arguments and returns how many elements there are common in both lists.
e.g. f [1, 2, 4, 2] [2, 3, 4, 4] returning 2 (repetitions are ignored).
Any suggestions? I tried this
*Main> a = [1, 2, 3]
*Main> b = [2, 3, 4]
*Main> [x | x <- a, x <- b]
[2,3,4,2,3,4,2,3,4]
Then I was planning to use the length function to know how many item there are in common.
You don't want to extract an x from both lists; extract from one list, and check if it is present in the other.
> a = [1,2,3]
> b = [4,3,2]
> [x | x <- a, x `elem` b]
[2,3]
> [x | x <- b, x `elem` a]
[3,2]
Note that the order in which items appear in the result depends on the order in which they appear in the list you pull from.
$contains: [1, 2] // #> [1, 2] (PG array contains operator)
$contained: [1, 2] // <# [1, 2] (PG array contained by operator)
I want to know what is the actual use of both above opeartor
SELECT ...
FROM ...
WHERE foo IN (11,22,33)
HAVING COUNT(*) = 3; -- that is, all 3 were found
(This assumes there is only one row for each item in the IN clause.)