Python将函数count识别为名称

4 投票
4 回答
26718 浏览
提问于 2025-04-17 22:06

我正在查看巴斯德研究所的Python教程,这个BDFL说这是最好的入门教程,但我有一个非常基础的问题。

教程中提到:

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

为什么会这样呢?

顺便说一下,我在Stack上搜索过,但没有找到任何相关的信息。

评论

看看1.1.3节的开头。你需要先输入from string import *。

>>> 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

我输入了。

回答

>>> 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

我输入了,但还是不行。

4 个回答

0

count 是字符串模块中的一个方法,这意味着在你使用这个函数之前,你需要在文件的顶部“导入”它,这样你的解释器才能明白你在说什么。你只需在文件的第一行添加 from string import count 这一行,它就应该能正常工作了。

0

这个方法count()是在string这个包里定义的。如果你想在自己的代码中使用这个方法,就需要先把它引入进来。

在使用这个方法之前,添加下面这一行代码就可以解决你的问题:

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
4

你链接的这个教程很旧了:

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

你可能在使用更新的Python版本(>= 3),在这个版本中,string模块里不再有像count这样的字符串函数了。以前我们有过:

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']

现在我们使用字符串的方法,这些方法直接在str这个类型里:

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

甚至可以这样:

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

虽然其他回答都是对的,但现在的Python版本提供了一种新的方式来使用count这个方法。这个方法不仅可以用在str(字符串)上,还可以用在任何类型的sequence(序列)上,具体可以参考文档中的说明

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

因为seq是一个字符串对象,所以它也有count这个方法。

撰写回答