Creating a (2,2,3) Numpy array [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm simply trying to create a (2,2,3) Numpy array, and have done the following:
a = np.array[[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]]
I however get the following error:
File "xyz.py", line 6, in <module>
[9,10,11]]]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
How can I solve this issue?
Thanks.

You forgot parens. You want
a = numpy.array([[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]])
You got the "no attribute '__getitem__'" error because putting square brackets after a symbol means "call this object's __getitem__() method with the stuff inside the brackets", this is what's happening when you do a simple dictionary lookup:
>>> a = {1: 2}
>>> a[1]
2
>>> a.__getitem__(1)
2

Related

How many list can we add inside list in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have declared a list and trying to print it. Here is my code:
list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
print(list1[3][2][2][2])
And i got the following errors:
Traceback (most recent call last):
File "C:/Users/ASUS/Desktop/Data/tr.py", line 2, in <module>
print(list1[3][2][2][2])
TypeError: 'int' object is not subscriptable
Process finished with exit code 1
You are pointing to the wrong index. It's not an error about the list declaration. It's an error about your printing. Please read the errors carefully before asking.
Here is what your print statement points to:
list1[3] -> ["hi", 15,[2,3,4,'5',[3,7,8]]]
list1[3][2] -> [2,3,4,'5',[3,7,8]]
list1[3][2][2] -> 4
So when you try to print list1[3][2][2][2] it tries to access 2nd index of int 4. Which is not subscriptable.
Open a REPL and see for yourself!
>>> list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
>>> list1[3][2][2]
4
>>> _
This is why list1[3][2][2][2] ends with an error: list1[3][2][2] is not a list.

AttributeError: 'tuple' object has no attribute 'readlines' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm new to python environments and I'm currently working on a ml project. While reading a CSV file using readlines function I'm getting "tuple has no attribute readlines". Please someone help me.....
my code is
data_file=(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
print(data_file.readlines())
error is
AttributeError: 'tuple' object has no attribute 'readlines'
You missed open:
data_file = open(r"C:\Users\Sury teja\temp\mnist_train.csv", "r")
print(data_file.readlines())
In the code you wrote, data_file is just a tuple, which looks like this:
('C:\\Users\\Sury teja\\temp\\mnist_train.csv', 'r')
The error here is caused by the parenthesis around two arguments:
(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
This is caused by the fact that parenthesis are serving more than one purpose in Python: they are used for calling - for example - functions, like in this example, but also for initializing immutable data structures called tuples.
By omitting the function name (open) you have initialized a tuple instead of calling the open function. And tuple, like your error suggests, has no attribute readlines.

AttributeError: 'str' object has no attribute '__name__' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is causing this error? Isn't it possible commenting out lines like this inside code?
for i in (Class_1, """Class_2, Class_3"""):
name = i.__name__
Class_1, Class_2 and Class_3 are classes declared before the upper code.
Error output:
> Traceback (most recent call last):
File "", line 2, in <module>
name = i.__name__
AttributeError: 'str' object has no attribute '__name__'
Process finished with exit code 1
Error message line edited to fit the example code
Remove the triple-quoted string """Class_2, Class_3""" to avoid iterating over it which is what you're doing in this case so it looks like for i in (Class_1,) (parenthesis are optional).
It seems you want to comment out those unnecessary sides, but please note that those triple-quotes strings technically aren't comments, so they can still affect the script in some areas you didn't intend.
What do you mean by
for i in (Class_1, """Class_2, Class_3"""):
When you iterate over this tuple, the second element is a string, thus causing the error.

Python parentheses mistake (?) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When I run this code
from numpy import linspace,arange
from pylab import plot, show
T=1.05
pp=[]
nn=[]
for V in arange (1,50):
P=(8*T)/(3*V-1)-3/(V**2)
a=((V-1/3)*(8/3)*(V**2))/((8/3)*T*(V**3)-6(V-1/3)**2)
pp.append(P)
nn.append(a)
plot(P,a)
show()
I get:
File "C:\Users\asus\Desktop\", line 8, in <module>
a=((V-1/3)*(8/3)*(V**2))/((8/3)*T*(V**3)-6(V-1/3)**2)
TypeError: 'int' object is not callable
And I don't know why.
Issue is in line -
a=((V-1/3)*(8/3)*(V**2))/((8/3)*T*(V**3)-6(V-1/3)**2)
You are using 6(V-1/3) , you need to use - 6*(V-1/3) , as -
a=((V-1/3)*(8/3)*(V**2))/((8/3)*T*(V**3)-6*(V-1/3)**2)

How to declare and fill an empty array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got an error using numpy.zeros, it seems like my value a can't be filled since i got an error:
track=2
a=np.zeros(shape=(3,2))
eps_real=a(Cp-0.5,2)/2*3.14*track
eps_imag=a(Cp-0.5,2*track)/2*3.14*track
tau=a(Cp-1,2)
print tau
My error when i ran is:
Traceback (most recent call last):
File "Main.py", line 35, in <module>
eps_real=a(Cp-0.5,2)/2*3.14*track
TypeError: 'numpy.ndarray' object is not callable
Collection members in Python use square brackets ([]), not parentheses. So your code should be:
eps_real=a[Cp-0.5,2]/2*3.14*track
eps_imag=a[Cp-0.5,2*track]/2*3.14*track
tau=a[Cp-1,2]
Parentheses are used for calling functions, hence the error message object is not callable

Categories