-5

in this statement

stop_words_index = [word_index.get(w) + 3 for w in stop_words]

word_index.get(w) is an int, but this statement generates

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

How to fix this?

bhomass
  • 3,414
  • 8
  • 45
  • 75
  • 1
    You should do `get(w, 0)`, in case `get` returns None when `w` is not found in `word_index` – user3483203 Jul 18 '18 at 19:40
  • 6
    `word_index.get(w)` is obviously not a int – PRMoureu Jul 18 '18 at 19:40
  • 1
    Yeah, it sounds like you are trying to access a hash that doesn't exist, hence the nonetype – David Jul 18 '18 at 19:41
  • 1
    Have a look at `[w for w in stop_words if w not in word_index]` – Patrick Haugh Jul 18 '18 at 19:41
  • you guys are too quick. I just discovered myself. – bhomass Jul 18 '18 at 19:43
  • 1
    You should always be wary when you use `get` without a second argument, especially embedded in a larger expression. If `w` is always going to exist, you should be doing `[w]` rather than `.get(w)`. If it isn’t always going to exist, you need to think about what value you want when it doesn’t, because often—as here—it’s going to be 0 or 1 or an empty string or something, not `None`. – abarnert Jul 18 '18 at 19:53

1 Answers1

2

word_index.get(w) is None if w cannot be found in dictionnary word_index.

You should do word_index.get(w, 0) if you want this value to be 0 when w is not found.

Or [word_index.get(w) + 3 for w in stop_words if w in word_index] if you want to skip the not found words.

Gelineau
  • 2,031
  • 4
  • 20
  • 30