I'd like to write a basic program which copy the content from variable 'a' to variable 'b' but in reverse order, e.g.: a="toy" to b="yot"
My code:
a="toy"
index= len(a)
indexm=0
new=" "
while(index>0):
new[indexm]==a[index]
index=index-1
indexm=indexm+1
print(new)
I've got the following error message:
IndexError: string index out of range
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-56-c909e83737f5> in <module>()
5
6 while(index>0):
----> 7 new[indexm]==a[index]
8 index=index-1
9 indexm=indexm+1
IndexError: string index out of range
I would like to solve it without using built-in functions to learn programmer thinking.
Thank you in advance
try this:
a="toy"
index= len(a)
indexm=0
new=""
while(index>0):
new += a[index-1]
index=index-1
indexm=indexm+1
print(new)
Related
code_arr=[]
for i in word_tokenize(student_code):
code_arr.append(i)
print(code_arr)
print(len(code_arr))
codet_arr=[]
for i in word_tokenize(teacher_code):
codet_arr.append(i)
print(codet_arr)
print(len(codet_arr))
for code_s in code_arr:
for code_t in codet_arr:
if code_s==code_t:
code_arr.remove(code_t)
else:
continue
The above code gives below error:
ValueError Traceback (most recent call last)
<ipython-input-13-4fe8ea0c7232> in <module>()
2 for code_t in codet_arr:
3 if code_s==code_t:
----> 4 code_arr.remove(code_t)
5 else:
6 continue
ValueError: list.remove(x): x not in list
Without knowing the content of the arrays it's difficult to duplicate but I suspect your problem is in modifying the content of an array that you're iterating over.
For clarity, I tend to build a set of values that I want to remove, then remove them in a separate loop, viz...
to_remove = set()
for code_s in code_arr:
for code_t in codet_arr:
if code_s == code_t:
to_remove.add(code_t)
for code in to_remove:
code_arr.remove(code)
Debugging is easier too as you can check the codes to be removed before removing them.
Ps. You don't need the 'else continue' on the end of a loop.
I think this will work :
for i in code_arr:
while i in code_t:
code_t.remove(i)
print(code_t)
I have file names as
When I iterate over them, it iterated in a string manner like:
1
10
11
.
.
19
2
20
.. so on. I hope you got this.
I want to iterate them over as integers not strings. Please help me write a function for it.
for i,file in enumerate(sorted(files),key=lambda x: int(os.path.splitext(file)[0]))
#CODE
But gives an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-f667164b9d6e> in <module>
----> 6 for i,file in enumerate(sorted(files),key=lambda x: int(os.path.splitext(file)[0])):
TypeError: 'key' is an invalid keyword argument for enumerate()
Please help me write a function for it. Thanks in advance.
You might try converting all your files to ints first, then sort them.
import os
files = ['0.pdf', '1.pdf', '12.pdf', '15.pdf', '3.pdf', '2.pdf', ]
for i, file_as_number in enumerate(sorted(int(os.path.splitext(file)[0]) for file in files)):
print(i, file_as_number)
files = ['0.pdf', '1.pdf', '12.pdf', '15.pdf', '3.pdf', '2.pdf' ]
fileDict= {}
SortedFiles = []
for i in range(len(files)):
fileDict[int(files[i].split('.')[0])] = files[i]
for i in sorted(list(fileDict.keys())):
SortedFiles.append(fileDict[i])
print (SortedFiles)
['0.pdf', '1.pdf', '2.pdf', '3.pdf', '12.pdf', '15.pdf']
I was trying to construct a one-hot encoding system by using Python 3.7 + Jupyter notebook, and I ran into an error like this:
TypeError Traceback (most recent call last)
<ipython-input-87-0fc633482cfd> in <module>
1 for i, sample in enumerate(samples):
----> 2 for j, word in list(enumerate(sample.split()))[:max_length]:
3 index = all_token_index.get(word)
4 result[i,j,index] = 1
TypeError: 'list' object is not callable
Here are the original code:
import numpy as np
samples = ['This is a dog','This is a cat']
all_token_index = {}
for sample in samples:
for word in sample.split():
if word not in all_token_index:
all_token_index[word] = len(all_token_index) + 1
max_length = 10
result = np.zeros(shape=(len(samples), max_length, max(all_token_index.values()) + 1 ))
for i, sample in enumerate(samples):
# Get keys and values in sample
for j, word in list(enumerate(sample.split()))[:max_length]:
index = all_token_index.get(word)
result[i,j,index] = 1
Could you please help me? Thanks in advance!
The correct answer was given by #Samwise and #Ronald in the comments. Like they said, I used a list variable in one deleted notebook cell. After restarting the notebook, the problem was fixed.
I am trying to slice each sentence in a list of sentences from [0:10] character of it.
Example of list of sentences: list name = sd_list
['I was born and brought up in Delhi.',
'I am using Dell Latitude E5140 laptop since 2012.',
'I work for ABC company since 2014.']
I tried to slice the first 10 characters of each sentence by running the below code and failed.
sent10 = [s[0:10] for s in sd_list]
By running this I encountered below TypeError
TypeError Traceback (most recent call last)
in ()
----> 1 [s[0:10] for s in sd_list]
in (.0)
----> 1 [s[0:10] for s in sd_list]
TypeError: 'float' object is not subscriptable
--> I even tried defining a function :
def sent_slice(text):
for s in range(0,len(text)):
text[s] = text[s][0:10]
return text
sent_slice(sd_list)
TypeError Traceback (most recent call last)
in ()
----> 1 sent_slice(sd_list)
in sent_slice(text)
1 def sent_slice(text):
2 for s in range(0,len(text)):
----> 3 text[s] = text[s][0:10]
4 return text
TypeError: 'float' object is not subscriptable
Could someone help me understand this "TypeError: 'float' object is not subscriptable" . How can I achieve my goal of slicing sentence?
it means that you have a float in sd_list. you can find it by doing something like:
print([f for f in sd_list if isinstance(f, float)])
I have written a code in python to generate a sequence of ARIMA model's and determine their AIC values to compare them.The code is as below,
p=0
q=0
d=0
for p in range(5):
for d in range(1):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
print(arima_mod.params)
print arima_mod.aic()
I am getting a error message as below,
TypeError Traceback (most recent call last)
<ipython-input-60-b662b0c42796> in <module>()
8 arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
9 print(arima_mod.params)
---> 10 print arima_mod.aic()
global arima_mod.aic = 1262.2449736558815
11
**TypeError: 'numpy.float64' object is not callable**
Remove the brackets after print arima_mod.aic(). As I read it, arima_mod.aic is 1262.2449736558815, and thus a float. The brackets make python think it is a function, and tries to call it. You do not want that (because it breaks), you just want that value. So remove the brackets, and you'll be fine.