Python将函数计数识别为名称

时间:2014-03-13 11:41:26

标签: python python-3.3

我正在查看Python tutorials from the Pascal institute BDFL说最好开始,我有一个非常基本的问题

tutorial中说:

How many of each base does this sequence contains?

>>> count(seq, 'a')
35
>>> count(seq, 'c')
21
>>> count(seq, 'g')
44
>>> count(seq, 't')
12

当我尝试做的是它不起作用

>>> count(seq, 'a')
Traceback (most recent call last):
  File "<pyshell#140>", line 1, in <module>
    count(seq, 'a')
NameError: name 'count' is not defined

为什么会这样?

  
    

我&#39;我已经搜索过了,我还没有找到任何东西。

  

COMMENT

看一下1.1.3节的开头。您必须先从字符串导入*

键入
>>> from string import*
>>> nb_a = count(seq, 'a')
Traceback (most recent call last):
  File "<pyshell#73>", line 1, in <module>
    nb_a = count(seq, 'a')
NameError: name 'count' is not defined
>>> from string import *
>>> nb_a = count(seq, 'a')
Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    nb_a = count(seq, 'a')
NameError: name 'count' is not defined

我做了。

ANSWER

>>> from string import *
>>> from string import count
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    from string import count
ImportError: cannot import name count
>>> from string import count
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    from string import count
ImportError: cannot import name count

我做到了。没&#39;工作。

4 个答案:

答案 0 :(得分:3)

您链接的教程非常陈旧:

Python 2.4.2 (#1, Dec 20 2005, 16:25:40) 

你可能正在使用更现代的Python(&gt; = 3),在这种情况下count模块中不再有像string这样的字符串函数。我们曾经有过

Python 2.7.5+ (default, Feb 27 2014, 19:39:55) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from string import count
>>> count("abcc", "c")
2

但今天:

Python 3.3.2+ (default, Feb 28 2014, 00:53:38) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from string import count
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name count
>>> import string
>>> dir(string)
['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
 '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__',
 '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase',
 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 
 'punctuation', 'whitespace']

这些天我们使用字符串 methods 代替str本身的字符串:

>>> 'abcc'.count('c')
2

甚至

>>> str.count('abcc','c')
2

答案 1 :(得分:1)

虽然其他答案是正确的,但是当前的Python版本提出了另一种调用count的方法,因为它可用于str,但也适用于任何类型的sequence,{{3} }:

>>> seq.count('a')
35

由于seq是字符串 object ,它也有count方法。

答案 2 :(得分:0)

此方法count()string包中定义。要在代码中使用此方法,您需要导入定义。 在使用该方法之前添加以下import语句将解决您的问题

from string import count

>>> seq='acdaacc'
>>> count(seq,'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'count' is not defined
>>> from string import count
>>> count(seq,'a')
3

答案 3 :(得分:0)

count是字符串模块中的一个方法,意味着在文件的顶部(在使用该函数之前),您需要“导入”它,以便您的解释器知道您在说什么。添加行from string import count作为文件的第一行,它应该可以正常工作。