How To Transpose matrix list - python

I am trying to Transpose matrix list but error:
def transpose_matriks(mat_a):
def wrapper():
temp_row = []
temp_mat = []
for i in range(0, pjg_matriks(mat_a)):
for j in range(0, lbr_matriks(mat_a)):
temp_row.append( mat_a[j][i])
temp_mat.append(temp_row)
temp_row = []
return temp_mat
return wrapper
#transpose_matriks
def matrixY():
X = [3,2,4,5]
return matrixY
print(matrixY())
erorr problem TypeError: object of type 'function' has no len() :
Traceback (most recent call last):
File "C:\Users\ASUS X441U\PycharmProjects\PfModul4\coba.py", line 23, in <module>
print(matrixX())
File "C:\Users\ASUS X441U\PycharmProjects\PfModul4\coba.py", line 4, in wrapper
rows = len (matrix)
TypeError: object of type 'function' has no len()
the output should be like this
matrixY = [3,4,2,5]

Related

Getting TypeError when setting spreasheet column value

for product_row in worksheet.iter_rows(min_row=2,
max_row=worksheet.max_row,
min_col=1,
max_col=5, values_only=True):
test_write = worksheet.cell(product_row,5)
test_write.value = 5
Gives me this error:
Traceback (most recent call last):
File "C:\pythonProject\main.py", line 62, in <module>
test_write = worksheet.cell(product_row,5)
File "C:\pythonProject\venv\lib\site-packages\openpyxl\worksheet\worksheet.py", line 237, in cell
if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'tuple' and 'int'
But when I have this:
for product_row in range(2, worksheet.max_row+1):
test_write = worksheet.cell(product_row,5)
test_write.value = 5
I do not get any error. What am I doing wrong?

I get the typeerror while executing the following program

def get_playlist_tracks(username, playlist_id):
results = sp.user_playlist_tracks(username,playlist_id)
playlist_items = results['items']
uris = []
while results['next']:
results = sp.next(results)
playlist_items.append(results['items'])
for item in playlist_items:
track_uri = item["track"]["uri"]
uris.append(track_uri)
return uris
get_playlist_tracks('iKetu', '7h10RuUjKqfqwWibESmwy1')
print(uris)
The Output I get is
PS C:\Users\iketu\Music\Elite\Album Artworks> & C:/Users/iketu/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/iketu/Music/Elite/Album Artworks/getAlbumArt.py"
Traceback (most recent call last):
File "c:/Users/iketu/Music/Elite/Album Artworks/getAlbumArt.py", line 27, in <module>
get_playlist_tracks('iKetu', '7h10RuUjKqfqwWibESmwy1')
File "c:/Users/iketu/Music/Elite/Album Artworks/getAlbumArt.py", line 23, in get_playlist_tracks
track_uri = item["track"]["uri"]
TypeError: list indices must be integers or slices, not str

python: typecast object in dict()

I have the following code
histos = dict()
for key in list:
obj = file.Get(key)
if not key in histos:
histos[key] = obj.Clone()
else
histos[key].Add(obj)
Add() is a function of obj, however I get the following error if the object is retrieved from a python dict():
Traceback (most recent call last):
File "./script.py", line 35, in <module>
histos[key].Add(obj)
AttributeError: 'PyROOT_NoneType' object has no attribute 'Add'
How can I typecast objects retrieved from dict()

TypeError: 'float' object is not callable error while calling a function

This is the code I currently have:
def uniform_distribution(users,radius):
user_coordinates_distance=[]
user_coordinates=[]
finding_shadowing=[]
r=radius*np.sqrt(np.random.uniform(0,1,users))
angle=2*np.pi*np.random.uniform(0,1,users)
x = r*np.cos(angle)
y = r*np.sin(angle)
user_distance = np.sqrt(x*x+y*y)
x_shadowing=1000*x
y_shadowing=1000*y
x_shadowing=(x_shadowing-x_shadowing%10)/10
y_shadowing=(y_shadowing-y_shadowing%10)/10
finding_shadowing=shadowing(x_shadowing,y_shadowing,shadowing_matrix)
print(finding_shadowing)
for i in range (0,users):
user_coordinates=[x[i],y[i],user_distance[i],finding_shadowing[i]]
user_coordinates_distance.append(user_coordinates)
return (user_coordinates_distance)
And this is the error I get when I run it:
Traceback (most recent call last):
File "C:\Users\Ajit\Desktop\maryland\Sem 2\ENTS 656\project656\main program and functions\main_1.py", line 136, in <module>
user_coordinates=uniform_distribution(attempts,cell_radius)#list [x,y,distance,shadowing]
File "C:\Users\Ajit\Desktop\maryland\Sem 2\ENTS 656\project656\main program and functions\main_1.py", line 81, in uniform_distribution
finding_shadowing=shadowing(x_shadowing,y_shadowing,shadowing_matrix)
TypeError: 'float' object is not callable
What exactly does this error mean?

Python TypeErrors: "' list' object is not callable" and "'function' object is unsubscriptable"

I have the following code:
from random import randint,choice
add=lambda x:lambda y:x+y
sub=lambda x:lambda y:x-y
mul=lambda x:lambda y:x*y
ops=[[add,'+'],[sub,'-'],[mul,'*']]
def gen_expression(length,left,right):
expr=[]
for i in range(length):
op=choice(ops)
expr.append([op[0](randint(left,right)),op[1]])
return expr
def eval_expression (expr,x):
for i in expr:
x=i[0](x)
return x
def eval_expression2 (expr,x):
for i in expr:
x=i(x)
return x
[snip , see end of post]
def genetic_arithmetic(get,start,length,left,right):
batch=[]
found = False
for i in range(30):
batch.append(gen_expression(length,left,right))
while not found:
batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
print evald_expression_tostring(batch[0],start)+"\n\n"
#combine
for w in range(len(batch)):
rind=choice(range(length))
batch.append(batch[w][:rind]+choice(batch)[rind:])
#mutate
for w in range(len(batch)):
rind=choice(range(length))
op=choice(ops)
batch.append(batch[w][:rind]+[op[0](randint(left,right)),op[1]]+batch[w][rind+1:])
found=(eval_expression(batch[0],start)==get)
print "\n\n"+evald_expression_tostring(batch[0],start)
When I try to call to call genetic_artihmetic with eval_expression as the sorting key, I get this:
Traceback (most recent call last):
File "<pyshell#113>", line 1, in <module>
genetic_arithmetic(0,10,10,-10,10)
File "/home/benikis/graming/py/genetic_number.py", line 50, in genetic_arithmetic
batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
File "/home/benikis/graming/py/genetic_number.py", line 50, in <lambda>
batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
File "/home/benikis/graming/py/genetic_number.py", line 20, in eval_expression
x=i[0](x)
TypeError: 'function' object is unsubscriptable
And when I try the same with eval_expression2 as the sorting,the error is this:
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
genetic_arithmetic(0,10,10,-10,10)
File "/home/benikis/graming/py/genetic_number.py", line 50, in genetic_arithmetic
batch=sorted(batch,key=lambda y:abs(eval_expression2(y,start)-get))
File "/home/benikis/graming/py/genetic_number.py", line 50, in <lambda>
batch=sorted(batch,key=lambda y:abs(eval_expression2(y,start)-get))
File "/home/benikis/graming/py/genetic_number.py", line 25, in eval_expression2
x=i(x)
TypeError: 'list' object is not callable
As far as i can wrap my head around this, my guess is that sorted() is trying to recursively sort the sublists,maybe? What is really going on here?
Python version is 2.6 - the one in the debian stable repos.
[snip] here:
def expression_tostring(expr):
expr_str=len(expr)*'('+'x '
for i in expr :
if i[1]=='*':
n=i[0](1)
else:
n=i[0](0)
expr_str+=i[1]+' '+str(n)+') '
return expr_str
def evald_expression_tostring(expr,x):
exprstr=expression_tostring(expr).replace('x',str(x))
return exprstr+ ' = ' + str(eval_expression(expr,x))
x=i[0](x) #here i is a function so you can't perform indexing operation on it
x=i(x) #here i is a list so you can't call it as a function
in both cases the value of i is fetched from expr, may be expr contains different type of object than what you're assuming here.
Try this modification:
def gen_expression(length,left,right):
expr=[]
for i in range(length):
op=choice(ops)
expr.append([op[0], randint(left,right),op[1]])
return expr
def eval_expression (expr,x):
for i in expr:
x=i[0](i[1])
return x
You had expr.append([op[0](randint(left,right)),op[1]]) which will put the return value of the calling the function into the 0th index.

Categories

Resources