Python : Slice each sentence in list of sentences - python

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)])

Related

Word2Vec error: TypeError: unhashable type: 'list'

I'm experimenting with peptide sequences and NLP right now and am trying to embed the peptide sequences using word2vec.
The peptides come in a long string format (ex: 'KCNTATCATQRLANFLVRSSNNLGPVLPPTNVGSNTY'), so I've split the peptide sequences into trigrams. But as I'm trying to embed them, I keep getting this error: TypeError: unhashable type:'list.'
Not sure how to fix this error as I don't quite understand why it's coming up. My code is linked here, and here is the full error output:
TypeError Traceback (most recent call last)
<ipython-input-17-966c68819734> in <module>()
6
7 # embeddings pos
----> 8 w2vpos = Word2Vec(kmersdatapos, size=EMB_DIM,window=5,min_count=5,negative=15,iter=10,workers=multiprocessing.cpu_count())
4 frames
/usr/local/lib/python3.7/dist-packages/gensim/models/word2vec.py in _scan_vocab(self, sentences, progress_per, trim_rule)
1553 )
1554 for word in sentence:
-> 1555 vocab[word] += 1
1556 total_words += len(sentence)
1557
TypeError: unhashable type: 'list'
Any suggestions are appreciated!
You need to pass a list of list of strings to gensim's Word2Vec. In your code you are passing kmersdatapos to Word2Vec, which is list of list of list of strings.
For example:
corpus = [["lorem", "ipsum"], ["dolor"], ["sit", "amet"]]
is a valid parameter for the Word2Vec function. Whereas,
corpus = [[["lorem", "ipsum"], ["dolor"]], [["sit", "amet"]]]
is invalid.

TypeError: list indices must be integers or slices, not tuple, Issue happens in a parallel corpus

The following is my dataset. It is a parallel corpus.
[['Service eka godaak hondai', 'සේවාව ගොඩාක් හොඳයි '],
['Kama rasai ', 'කෑම රසයි '],
['Godak fresh and maarama rasaii, perfect. ', 'ඉතා රසවත් හා ඉතා රසවත්, පරිපූර්ණයි'],
['place eka piliwelai, clean , kama godaak rasai', 'ස්ථානය පිළිවෙලට, පිරිසිදුයි, කෑම හරිම රසයි']]
when I try this following code
singlish_tokenizer = create_tokenizer(dataset[:,0])
This error below is shown
TypeError Traceback (most recent call last)
<ipython-input-138-74d68e010cad> in <module>
3 # a[:stop] # items from the beginning through stop-1
4 #a[:] means a copy of the whole array
5 singlish_tokenizer = create_tokenizer(dataset[:,0])
TypeError: list indices must be integers or slices, not tuple
this is the create_tokenizer method
def create_tokenizer(lines):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(lines)
return tokenizer
Can anyone please help me in this issue. I tried different solutions but nothing worked. I am stuck with this code.

Basic in Python

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)

Python Error :'numpy.float64' object is not callable

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.

Why can't I call a Kivy.core.image function?

I did a program using python and pyGTK. Now, I'm translating it to Kivy, for use it with a tablet (Android). One archive.py is a custom widget with graphic information about a person (an image of her/his, a frame with color if him/his is selected, a little candle if is your birthday,..)
The question is, to make the custom widget I need to know the image size, but when I write:
wi = self._imagen.width()
I have this error:
TypeError: 'int' object is not callable
Before, in code, I check self._imagen is an kivy.core.image.Image object, that has a width function:
if self._imagen != None:
print self._imagen #terminal print <kivy.core.image.Image object at 0xb2f2db9c>
wi = self._imagen.width() #ERROR
hi = self._imagen.height()
I search for any instruction in my code like: self._imagen = 1 (or other int number) (there is not).
I don't know why. Thanks and sorry for my english
Use wi = self._imagen.width using wi = self._imagen.width() is trying to call the int object as a method, that is why you are getting the error.
In [1]: width = 3
In [2]: print width
3
In [3]: print width()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-33019bd92b32> in <module>()
----> 1 print width()
TypeError: 'int' object is not callable

Categories