I wrote this code (this is only a part of it which was causing the error):
weight_bag_plus = ind_weight_bag + count
weight_bag_minus_1 = ind_weight_bag + count - 1
ind_plus = weight_bag[weight_bag_plus]
ind_plus_minus_1 = weight_bag[weight_bag_minus_1]
if ind_plus < ind_plus_minus_1:
count += 1
and it gave me this error:
Execution failed.
TypeError : 'int' object is not subscriptable
Stack Trace:
Traceback (most recent call last):
File "/tmp/user_code.py", line 17, in <module>
ind_plus = weight_bag[weight_bag_plus]
TypeError: 'int' object is not subscriptable
How do I Solve the Error?
This means that weight bag is an integer, so you cannot select an index from it or slice it. Ensure that your weight_bag variable is a list, tuple, or dictionary.
Related
My data are stored in different directories on google drive. I want to extract one certain text file from each directory and store them as a single csv file. The csv file called model keeps all the different file names that I need to get. And this is the only part I need to change for searching the files that are qualified.
To be more specific:the model csv file contains the following :['ENS','ENS_hr','ENS_lr','MM5','MM5G','MPAS25','NMM3GFS','NMM3NAM','WRF2GFS','WRF2GFS81','WRF2NAM','WRF2NAM81','WRF3ARPEGE','WRF3GEM','WRF3GFS','WRF3GFSgc01','WRF3NAM','WRF3NAVGEM','WRF4ICON','WRF4NAM','WRF4RDPS']
here is my code:
md = []
model = pd.read_csv(verification_path + 'model_name.csv')
#find file for the correct model
for m in model.iterrows():
model_file = verification_path + m +'/MAE_monthly_APCP6_60hr_small.txt'
new = pd.read_csv(model_file)
md.append(new)
But I got the error shows:
TypeError Traceback (most recent call last)
<ipython-input-5-981115533dbc> in <module>
6 #find file for the correct model
7 for m in model.iterrows():
----> 8 model_file = verification_path + m +'/MAE_monthly_APCP6_60hr_small.txt'
9 new = pd.read_csv(model_file)
10 md.append(new)
TypeError: can only concatenate str (not "tuple") to str
Does anyone have any idea how to solve it? Is there another better way?
Thx!
I tried to convert the tuple by the following code and got the new error:
The code for converting tuple:
import functools
import operator
def convertTuple(tup):
str = functools.reduce(operator.add, (tup))
return str
The updated code:
for m in model.iterrows():
model_file = verification_path + convertTuple(m) +'/MAE_monthly_APCP6_60hr_small.txt'
new = pd.read_csv(model_file)
md.append(new)
The error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/ops/array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
165 try:
--> 166 result = func(left, right)
167 except TypeError:
12 frames
TypeError: unsupported operand type(s) for +: 'int' and 'str'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/roperator.py in radd(left, right)
7
8 def radd(left, right):
----> 9 return right + left
10
11
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I am getting this error and not able to resolve it and not able to find it on the internet.
TypeError: 'numpy.float64' object cannot be interpreted as an integer
TypeError Traceback (most recent call last)
<ipython-input-10-33f2a17ec582> in <module>
20 print("Saving New CSV file")
21 if __name__=='__main__':
---> 22 dataSetExtraction()
<ipython-input-10-33f2a17ec582> in dataSetExtraction()
6 dfReal=processRealNewsDataFrame(dfReal)
7 dfCombine=[]
----> 8 for d in extractTopRealResultsForCrawling(dfReal):
9 print('len of datadrame :',d['URL'].size)
10 #d=d[:100]
<ipython-input-6-9dbfd3f21499> in extractTopRealResultsForCrawling(dfReal)
6 listOfIndex=[]
7 df=[]
----> 8 for i in range(0,loop):
9 listOfIndex.append(dfReal[i*10000:(i+1)*10000])
10 df+=[dfReal[i*10000:(i+1)*10000]]
TypeError: 'numpy.float64' object cannot be interpreted as an integer
This is code giving the error. I have not been able to remove the error Please help me
def extractTopRealResultsForCrawling(dfReal):
print("Retrieve top 20000 Real news data")
num=dfReal.size
loop=num/10000
listOfIndex=[]
df=[]
for i in range(0,loop):
listOfIndex.append(dfReal[i*10000:(i+1)*10000])
df+=[dfReal[i*10000:(i+1)*10000]]
#print "length of dataframe array retrieved:",len(df[0])
return df[:LEN]
The range function can only receive integer values
Here is a minimal code reproducing (more or less) the problem:
>>> a = 2.0
>>> [i for i in range(a)]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
[i for i in range(a)]
TypeError: 'float' object cannot be interpreted as an integer
You need to convert the value to an integer
>>> [i for i in range(int(a))]
[0, 1]
In your code you should use:
for i in range(int(loop)):
Alternatively, you could do:
for i in range(0, num, 10000):
listOfIndex.append(dfReal[i:i+10000])
df+=[dfReal[i:i+10000]]
avoiding the division...
i have variable a
a = 349
i can do float(a)
output is:
349.0
i can do int(a)
output is:
349
but with str(a) i have error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-152-bddfa438ffc2> in <module>
----> 1 str(a)
TypeError: 'str' object is not callable
why? str is basic method as i know. whats i'm doing wrong
The code:
a = 349
a = int(a)
a = float(a)
str(a)
Works in jupyter as well as terminal repl.
Did you name another variable str = 'whatever xyz' somewhere in your code?
I have built a code for a codewars problem. I think it is correct but it shows me an error I don't understand.
Can you tell me what am I doing wrong?
import math
def waterbombs(fire, w):
s=""
countx=0
for i in fire:
if i=="x":
countx+=1
elif i=="Y":
countx=0
return sum(math.ceil(countx/w))
waterbombs("xxYxx", 3)
This is the error:
Traceback (most recent call last):
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 16, in <module>
waterbombs("xxYxx", 3)
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 13, in waterbombs
return sum(math.ceil(countx/w))
TypeError: 'int' object is not iterable
[Finished in 0.2s]
Why are you doing sum(math.ceil(countx/w)) ?
What is the objective of the sum method here, since there is only value returned by math.ceil ?
The sum would throw that error if you pass a single value to it. You're supposed to pass a list of values of the sum method.
For eg: sum(5) would give you the same error you see above, but sum([5]) would return you 5.
I am working on a homework problem, and got the following error:
Traceback (most recent call last):
File "/Users//Dropbox/Homework 3 - 2.py", line 15, in <module>
conversion = convert_feet_to_inches(get_feet)
File "/Users//Dropbox/Homework 3 - 2.py", line 4, in convert_feet_to_inches
calculate_conversion = feet*12 TypeError:
unsupported operand type(s) for *: 'function' and 'int'
Here is my code. I'm trying to convert feet to inches:
def convert_feet_to_inches(feet):
calculate_conversion = feet*12
return calculate_conversion
def get_feet():
ask_for_feet = float(input("Please enter number of feet for conversion "))
return ask_for_feet
def printing_answer():
print (convert_feet_to_inches)
asking_for_feet = get_feet()
conversion = convert_feet_to_inches(get_feet)
print_answer(printing_answer)
What am I doing wrong?
I think you meant to pass asking_for_feet to convert_feet_to_inches instead of the function get_feet in this line:
conversion = convert_feet_to_inches(get_feet)
So that should be:
conversion = convert_feet_to_inches(asking_for_feet)
The error was because get_feet() is a function and you passed it to convert_feet_to_inches() which is taking the argument you pass it and multiplying it by 12. You can't multiply a function by an int so that is what the error was saying. I think what you meant to do was to pass asking_for_feet. So change
conversion = convert_feet_to_inches(get_feet)
to
conversion = convert_feet_to_inches(asking_for_feet)
After that, you have
print_answer(printing_answer)
The function print_answer was not defined yet so change:
def printing_answer():
print (convert_feet_to_inches)
to
def print_answer(answer):
print (answer)
Then you final line of code would be:
print_answer(conversion)