0

I am trying to iterate through the "cutoff" parameter of the difflib.get_close_matches using the following code:

import difflib
from numpy import loadtxt

text_file = open('filewithtext.txt', "r")
lines = text_file.read().split(',')

word = 'word_to_search_in_file'

for i in range(0,0.4):
difflib.get_close_matches(word, lines, n=10, cutoff=i)

I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-224-a5dc535e6b28> in <module>()
2 word = 'word_to_search_in_file'
3 
----> 4 for i in range(0,0.4):
5     difflib.get_close_matches(word, lines, n=10, cutoff=i)

TypeError: 'float' object cannot be interpreted as an integer

My question is - how can iterate through the cutoff parameter, to get the function runs for a list of numbers assigned to the cutoff

Thanks.

Eyal
  • 3
  • 2
  • why do you have a float 0.4 in your range rather then 4 ? – user1767754 Nov 21 '17 at 22:14
  • 1
    Try changing the range to `range(0,4)` and then change the cutoff value to `cutoff=i/10.0`. It is better not to have floating point numbers in `range` function. – Vasilis G. Nov 21 '17 at 22:14
  • Thanks for that. The problem is that now I don't get error, but I don't get anything else, just an empty return. If I run this function alone ("as is") with no loops, I do get the info for the same parameters. – Eyal Nov 21 '17 at 23:10
  • Can you provide a sample input of `word` and a sample input of `lines`? – Vasilis G. Nov 21 '17 at 23:25
  • sample input: create a text file with the word "word_to_search_in_file", name the file "filewithtext.txt" Then run the code without the loop, and you get a response: "['sampleword']" If you run it with the loop, you get nothing – Eyal Nov 21 '17 at 23:29

0 Answers0