Why can't I use string functions inside map ()?

The following example shows the error that I get when I try to use a string function inside a function call for matching. I need help on why this is happening. Thank.

>>> s=["this is a string","python python python","split split split"]
>>> map(split,s)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    map(split,s)
NameError: name 'split' is not defined

Although split()a built-in function, but does it still cause this error?

+5
source share
2 answers

It will work fine if you use str.split()

i.e,

s = ["this is a string","python python python","split split split"]
map(str.split, s)

gives:

[['this', 'is', 'a', 'string'],
 ['python', 'python', 'python'],
 ['split', 'split', 'split']]

The error message states:, therefore, the interpreter does not recognize , because it is not a built-in function . To do this, you need to associate with the inline object . NameError: name 'split' is not definedsplitsplitsplitstr

: @Ivc /.

+14

split , str.split . , split str, .

:

>>> str
<type 'str'>
>>> str.split
<method 'split' of 'str' objects>
+2

All Articles