TypeError: slice indices must be integers or None or have an __index__ method. How to resolve it?

2024/10/14 7:13:53
 if w<h:normalized_char = np.ones((h, h), dtype='uint8')start = (h-w)/2normalized_char[:, start:start+w] = charelse:normalized_char = np.ones((w, w), dtype='uint8')start = (w-h)/2normalized_char[start:start+h, :] = char

Running this on jupyter and getting this error

<ipython-input-8-15d17de04b9c> in extractCharactersNew(function)60             normalized_char = np.ones((h, h), dtype='uint8')61             start = (h-w)/2
---> 62             normalized_char[:, start:start+w] = char63         else:64             normalized_char = np.ones((w, w), dtype='uint8')TypeError: slice indices must be integers or None or have an __index__ method

How Can I resolve this error?

Answer

Your error is:-

TypeError: slice indices must be integers or None or have an __index__ method

slice indices, in your case refer to the variables that you are using to slice the list in

normalized_char[:, start:start+w] = char

That is - start and start+w. For list slicing, these must be integers or have an __index__ method. This __index__ method is a special method that returns integer value for that object. You should be able to solve your issue by ensuring that you provide correct slice indices. You can use start = (h-w)//2(integer division) to make sure that start is an integer.

https://en.xdnf.cn/q/117981.html

Related Q&A

Keras: Understanding the number of trainable LSTM parameters

I have run a Keras LSTM demo containing the following code (after line 166):m = 1 model=Sequential() dim_in = m dim_out = m nb_units = 10model.add(LSTM(input_shape=(None, dim_in),return_sequences=True,…

Updating Labels in Tkinter with for loop

So Im trying to print items in a list dynamically on 10 tkinter Labels using a for loop. Currently I have the following code:labe11 = StringVar() list2_placer = 0 list1_placer = 1 mover = 227 for items…

Paginate results, offset and limit

If I am developing a web service for retrieving some album names of certain artist using an API, and I am asked:The service should give the possibility to paginate results. It should support ofset= and…

Improve code to find prime numbers

I wrote this python code about 3 days ago, and I am stuck here, I think it could be better, but I dont know how to improve it. Can you guys please help me?# Function def is_prime(n):if n == 2 or n == …

How to read the line that contains a string then extract this line without this string

I have a file .txt that contains a specific line, like thisfile.txt. . T - Python and Matplotlib Essentials for Scientists and Engineers . A - Wood, M.A. . . .I would like to extract lines that contain…

Python: How to access and iterate over a list of div class element using (BeautifulSoup)

Im parsing data about car production with BeautifulSoup (see also my first question):from bs4 import BeautifulSoup import stringhtml = """ <h4>Production Capacity (year)</h4>…

What should I worry about Python template engines and web frameworks? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Value Search from Dictionary via User Input

I have written the following code for getting an output of the various districts located in the given city and their respective postal codes. I want my code to be able to receive input from the user (D…

Read and aggregate data from CSV file

I have a data file with the following format:name,cost1,cost1,cost1,cost2,cost3,cost3, X,2,4,6,5,6,8, Y,0,3,6,5,4,6, . . ....Now, what I would like to do is to convert this to a dictionary of dictionar…

nltk cant using ImportError: cannot import name compat

This is my codeimport nltk freq_dist = nltk.FreqDist(words) print freq_dist.keys()[:50] # 50 most frequent tokens print freq_dist.keys()[-50:] # 50 least frequent tokensAnd I am getting this error mess…