How to append list in list in python3? - python

def cal(i,L,cset,comp):
for n in range(i,len(L)):
print(cset+[L[1]])
comp.insert(cset+[L(n)])
cal(i+1,L,comp[len(comp)-1],comp)
return comp
print(cal(0,[1,2,3,4,5],[],[]))
in this code it gives me an error from the first iterration
line 4, in cal
comp.insert(cset+[L(n)])
TypeError: 'list' object is not callable

I did change
comp.insert(cset+[L(n)])
to
comp.insert(cset+[L[n]])
Just got lost there :/

Related

I'm getting 'int' object is not subscriptable

I'm new to python I'm trying to implement a code for my project at first my error code was object of type 'int' has no len() this was my code and added str to solve the issue
xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
Now I'm getting 'int' object is not subscriptable if in this line
xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
If I change it to
xored_value = ord(Mblocks(i%len(str(Mblocks)))=)) ^ ord(Cblocks(i%len(str(Cblocks))))
I get 'str' object is not callable.
Here's my full function :
def xor_two_str(Mblocks,Cblocks):
xored = []
for i in range(max(len(str(Mblocks)), len(str(Cblocks)))):
xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
xored.append(hex(xored_value)[2:])
return ''.join(xored)
Any help please?
So, I am sure the Mblocks and Cblocks parameters you are passing are integers. Since it is being an integer, if you try to slice part of it you will get TypeError
> 100[1]
TypeError: 'int' object is not subscriptable
Lets do a piece by piece inspection.
Here in the first approach:
> xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
> error1 = len(Mblocks)
TypeError: object of type 'int' has no len()
since Mblocks is integer, integer doesn't have len function
In change 2,
you corrected the first error but:
> xored_value = ord(Mblocks[i%len(str(Mblocks))]) ^ ord(Cblocks[i%len(str(Cblocks))])
> error2 = Mblocks[i%len(str(Mblocks))]
> error2 = Mblocks[some_int]
TypeError: 'int' object is not subscriptable
In change 3:
xored_value = ord(Mblocks(i%len(str(Mblocks)))=)) ^ ord(Cblocks(i%len(str(Cblocks))))
> error3 = Mblocks(callingWithParameter)
Simply in python something(withbraces) is calling something. Same happened here
So the easiest solution is make Mbraces and Cbraces of before you process anything down Like here is the solution:
def xor_two_str(Mblocks,Cblocks):
Mblocks = str(Mblocks)
Cblocks = str(Cblocks)
xored = []
for i in range(max(len(Mblocks), len(Cblocks))):
xored_value = ord(Mblocks[i%len(Mblocks)]) ^ ord(Cblocks[i%len(Cblocks)])
xored.append(hex(xored_value)[2:])
return ''.join(xored)
Python expects strings for the ord() function, and this:
Mblocks[i%len(str(Mblocks))]
is an attempt to access element with index i%len(str(Mblocks)) from int Mblocks, which Python does not allow.
As such, you could do a str conversion at the beginning of your function and work with the converted variables from that point onwards.
def xor_two_str(Mblocks,Cblocks):
str_Mblocks=str(Mblocks)
str_Cblocks=str(Cblocks)
xored =[]
for i in range(max(len(str_Mblocks), len(str_Cblocks))):
xored_value = ord(str_Mblocks[i%len(str_Mblocks)]) ^ ord(str_Cblocks[i%len(str_Cblocks)])
xored.append(hex(xored_value)[2:])
return ''.join(xored)

'Tuple' object is not callable - Python

I'm using pygame and I'm using a function that set the selected position of a text
in PyGame :
def textPos(YPos , TextSize):
TextPosition.center(60,YPos)
print("Size : " + TextSize)
but when I run the program, I get an error:
TextPosition.center(60,YPos) : TypeError : 'Tuple' object is not callable
There's a way to solve this problem?
'Tuple' object is not callable error means you are treating a data structure as a function and trying to run a method on it. TextPosition.center is a tuple data structure not a function and you are calling it as a method. If you are trying to access an element in TextPosition.Center, use square brackets []
For example:
foo = [1, 2, 3]
bar = (4, 5, 6)
# trying to access the third element with the wrong syntax
foo(2) --> 'List' object is not callable
bar(2) --> 'Tuple' object is not callable
# what I really needed was
foo[2]
bar[2]

Attribute Error: list object has no attribute 'apply'

time_weight = list(100*np.exp(np.linspace(-1/divisor, -(num_steps-1)/divisor, num_steps))).apply(lambda x:int(x))
When I try this, I get the following error in Python 3.7.
AttributeError: 'list' object has no attribute 'apply'
Can anyone help with this?
As the error said, list type has no apply attribute.
This said, if you have a list l and you want to set to int type every element in it you may use:
l = [int(x) for x in l]
or
l = list(map(int,l))
As the error suggests, list has no apply method. If what you want to do is convert every element to an int, you could remove the lambda function and instead use astype(int):
time_weight = list((100*np.exp(np.linspace(-1/divisor, -(num_steps-1)/divisor, num_steps))).astype(int))

TypeError: 'dict_values' object is not subscriptable

best_params_train = dict(optimizer=optimizers[0], learning_rate=learning_rates[0],
cnn_train_type=cnn_train_types[0],
cnn_arch=cnns_arch.values()[0],
dropout=dropouts[0])
it gives error at cnn_arch=cnns_arch.values()[0] as TypeError: 'dict_values' object is not subscriptable.
i tried converting into list but not worked.
how to convert the above dict(....) into list
exp_params_train = dict(optimizer=optimizers[1:], learning_rate=learning_rates[1:],
cnn_train_type=cnn_train_types[1:], dropout=dropouts[1:],
cnn_arch=cnns_arch.values())
Quite on the contrary, it will work if you convert the dict_values object to a list:
cnn_arch=list(cnns_arch.values())[0]

Can anybody help to resolve the TypeError: 'float' object is not callable' in following code

I am writing a function for velocity and acceleration in the code below:
from math import exp
def kinematics(x,t,dt=1E-4):
x=x(t)
v_x=(x(t+dt)-x(t-dt))/(2*dt)
a_x=(x(t+dt)-2*x(t)+x(t-dt))/(dt**2)
return x,v_x,a_x
x=lambda t:exp(-(t-4)**2)
print(kinematics(x,5,dt=1E-5))
However I get the following error:
TypeError: 'float' object is not callable
Can anybody kindly point out the mistake?
You're reassigning your lambda to the return value of the lambda.
x=x(t)
After this point, x is no longer a lambda, it's a float.
You are redining x in the first line of the function:
def kinematics(x,t,dt=1E-4):
x=x(t)
...
Use this:
def kinematics(x,t,dt=1E-4):
v_x=(x(t+dt)-x(t-dt))/(2*dt)
a_x=(x(t+dt)-2*x(t)+x(t-dt))/(dt**2)
return x(t),v_x,a_x

Categories