56979

Python TypeError: sequence item 0: expected str instance, NoneType found

Question:

I copied this code from a book:

lyst = {'Hi':'Hello'} def changeWord(sentence): def getWord(word): lyst.get(word, word) return ''.join(map(getWord, sentence.split()))

I get this error:

Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> cambiaPronome('ciao') File "C:\Python33\2.py", line 6, in cambiaPronome return ''.join(map(getWord, list(frase))) TypeError: sequence item 0: expected str instance, NoneType found

What is wrong?

Answer1:

The getWord function doesn't explicitly return anything, so it implicitly returns None. Try returning something, e.g.:

def getWord(word): return lyst.get(word, word)

Answer2:

The problem is you're trying to write to a string a type which is NoneType. That's not allowed.

If you're interested in getting the None values as well one of the things you can do is convert them to strings.

And you can do it with a list comprehension, like:

return ''.join([str(x) for x in map(getWord, sentence.split())])

But to do it properly in this case, you have to return something on the inner function, else you have the equivalent to return None

Recommend

  • Continuously exchange data using python-shell
  • How to pickle numpy's Inf objects?
  • Error importing scipy.linalg on windows (python 3.3)
  • pandas series can't get index
  • Aspect advising other aspects
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Defining variable by logical subseting on time interval in data.table
  • ValueError: Found arrays with inconsistent numbers of samples
  • Partial specialization of a class template in derived class affects base class
  • user data scripts fails without giving reason
  • Cannot find “Grammar.txt” in python-sphinx
  • Rodeo UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal
  • Python 3.2.2, error(scripts to exe)
  • pillow imaging ImportError
  • Django simple Captcha “No module named fields” error
  • How to know when stdin is empty if it contains EOF?
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?