AttributeError: 'int' object has no attribute 'count' in dictionary - python-3.x

my question is that i am trying to get dictionary done in google colab but it is getting error of AttributeError: 'int' object has no attribute 'count' in dictionary again and again.
temp_dict = {"33": [temperatures.count(33)],"34": [temperatures.count(34)],"39": [temperatures.count(39)], "40": [temperatures.count(40)], "42": [temperatures.count(42)],"29": [temperatures.count(29)]}
this is my code line but it is not able to count in google colab.is there something missing.

From what I'm getting is that variable temperatures is not a list but the int
temperatures = [1,2,3,3,4]
print(temperatures.count(1)) # Returns 1
print(temperatures.count(3)) # Returns 2
print(temperatures.count(5)) # Returns 0
# Possible your code
temperatures = 33
print(temperatures.count(33)) # Returns error: AttributeError: 'int' object has no attribute 'count'
If you have python 3.11, it will highlight in which part your code error was made, for example
Traceback (most recent call last):
File "c:\Users\******\Desktop\nf\a.py", line 2, in <module>
print(temperatures.count(33))
^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'count'

Related

AttributeError: 'function' object has no attribute 'isna'

After applying isna to my original dataset, and saving it to a new variable.
That new variable is not acting like a dataframe and the output shows this error (AttributeError: 'function' object has no attribute 'isna') when I look for its shape.
When I read the new dataframe, it gives the description of the new dataframe in the output.
df1Books = df1.dropna
print(df1Books)
It is giving description of the new variable df1Books
And
df1Books.head()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-8f073fd0d9cc> in <module>
----> 1 df1Books.head()-tt
AttributeError: 'function' object has no attribute 'head'

TypeError: 'tuple' object is not callable when converting a list to tuple as return value

I want to convert the final list as tuple. However i am receiving an error.How can i get rid of this?
li= [(19343160,),(39343169,)]
def render_list_sql(li):
l = []
for index, tuple in enumerate(li):
idd = str(tuple[0])
l.append(idd)
return tuple(l)
print(render_list_sql(li))
Expected value to be returned is:
(19343160,39343169)
Error
Traceback (most recent call last):
File "test.py", line 20, in <module>
print(render_list_sql(list))
File "test.py", line 14, in render_list_sql
return tuple(l)
TypeError: 'tuple' object is not callable
As commented, don't use names for variables that mean other things to Python. This is called "shadowing" and you lose the meaning of the original name.
Example:
>>> tuple # This is the class used to create tuples.
<class 'tuple'>
>>> for index,tuple in enumerate([1,2,3]): # This is similar to your code
... print(index,tuple)
...
0 1
1 2
2 3
>>> tuple # tuple is no longer a class, but an instance of an integer.
3
>>> tuple([1,2,3]) # so this fails
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'int' object is not callable
>>> 3([1,2,3]) # You are basically doing this:
<interactive input>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'int' object is not callable
So don't do that:
li = [(19343160,),(39343169,)] # don't reassign list
def render_list_sql(li):
l = []
for index, tup in enumerate(li): # don't reassign tuple
idd = str(tup[0])
l.append(idd)
return tuple(l) # now this will work
print(render_list_sql(li))
Output:
('19343160', '39343169')
FYI, a shorter version using a generator:
li = [(19343160,),(39343169,)]
tup = tuple(str(i[0]) for i in li)
print(tup)

What is the correct call for lists

Why is every time I set a variable as a list, it always comes back as TraceBack error when I try to append to the list:
>>> a = list
>>> a.append('item1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'append' requires a 'list' object but received a 'str'
>>> type(a)
<class 'type'>
list is not the attribute to define lists but it is a method to convert to lists. It is explained here.
What is the python attribute to define a variable as a list?
To initialize a var with a certain type, use () after the name of the class:
a = list()
You can also instatiate a list like this:
mylist = []

AttributeError: Python 'filter' object has no attribute 'sort'

I am having issues with
AttributeError: 'filter' object has no attribute 'sort'
Below is the whole error message:
Using TensorFlow backend.
Traceback (most recent call last):
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 231, in <module>
n_imgs=15*10**4, batch_size=32)
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 166, in keras_fit_generator
data_to_array(img_rows, img_cols)
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 48, in data_to_array
fileList.sort()
AttributeError: 'filter' object has no attribute 'sort'
Process finished with exit code 1
def data_to_array(img_rows, img_cols):
clahe = cv2.createCLAHE(clipLimit=0.05, tileGridSize=(int(img_rows/8),int(img_cols/8)) )
fileList = os.listdir('TrainingData/')
fileList = filter(lambda x: '.mhd' in x, fileList)
fileList.sort()
In python 3 filter returns iterable. and you are calling sort method on iterable hence getting an error.
Either wrap iterable in list
fileList = list(filter(lambda x: '.mhd' in x, fileList))
or instead of fileList.sort() pass iterable in a sorted method
fileList= sorted(fileList)
Python 3 doc for filter
You are using Python 3. Filter returns an iterable filter object, but it has not sort method. Wrap the filter object in list.
fileList = list(filter(lambda x: '.mhd' in x, fileList))

Itertuples() in Tkinter function reveals an AttributeError: 'tuple' object has no attribute 'A'

Within a Tkinter function, I need to create the list named: 'value' extracting every 10 rows the value of dataframe column named: df['A'].
The following for-loop works perfectly out of a Tkinter function:
value = []; i = 0
for row in df.itertuples():
i = 1 + i
if i == 10:
value_app = row.A
value.append(value_app)
i=0
However within Tkinter function I have the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/anaconda/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "<ipython-input-1-38aed24ba6fc>", line 4174, in start
dfcx = self.mg(a,b,c,d,e)
File "<ipython-input-1-38aed24ba6fc>", line 4093, in mg
value_app = r.A
AttributeError: 'tuple' object has no attribute 'A'
A similar for-loop structure is running in another part of the same Tkinter function and is executed without errors.
If the column A is your first column you can do :
value_app = row[0]
I had the same problem and I think that it only sees it as regular arrays

Resources