Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将函数count识别为一个名称_Python_Python 3.3 - Fatal编程技术网

Python将函数count识别为一个名称

Python将函数count识别为一个名称,python,python-3.3,Python,Python 3.3,我看到的是,说什么是最好的开始,我有一个非常基本的问题 而在工作中,他说: 当我试着去做的时候,它不起作用 >>> count(seq, 'a') Traceback (most recent call last): File "<pyshell#140>", line 1, in <module> count(seq, 'a') NameError: name 'count' is not defined >>计数(序号“a”) 回溯(最

我看到的是,说什么是最好的开始,我有一个非常基本的问题

而在工作中,他说:

当我试着去做的时候,它不起作用

>>> count(seq, 'a')
Traceback (most recent call last):
  File "<pyshell#140>", line 1, in <module>
    count(seq, 'a')
NameError: name 'count' is not defined
>>计数(序号“a”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
计数(序号“a”)
NameError:未定义名称“count”
为什么会这样

顺便说一句,我已经搜索了堆栈资源,但没有找到任何东西

评论


请看第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
来自字符串导入的
>>*
>>>nb_a=计数(序号“a”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
nb_a=计数(序号“a”)
NameError:未定义名称“count”
>>>从字符串导入*
>>>nb_a=计数(序号“a”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
nb_a=计数(序号“a”)
NameError:未定义名称“count”
是的

答复

>>> 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
来自字符串导入的
>>*
>>>从字符串导入计数
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
从字符串导入计数
ImportError:无法导入名称计数
>>>从字符串导入计数
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
从字符串导入计数
ImportError:无法导入名称计数
是的。无效。

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

来自字符串导入计数

>>> 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
>>seq='acdaacc'
>>>计数(序号,a)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“count”
>>>从字符串导入计数
>>>计数(序号,a)
3.

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

虽然其他答案是正确的,但当前的Python版本提出了另一种调用
count的方法,因为它可以用于
str
,也可以用于任何类型的
序列,如下所示:


由于
seq
是字符串对象,因此它还具有
count
方法。

您链接到的教程非常古老:

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.count('abcc','c')
2

请看第1.1.3节的开头部分。您必须首先从字符串导入中键入
*
。。。。它没有说明
无法导入名称计数
您不能导入它两次,您的代码正在这样做。您还使用了一个非常旧(而且编写得很糟糕)的教程。如果您使用的是Python 3,您应该有一个该版本的教程。它说,
无法导入名称计数
@Codo您使用的是哪个版本的Python?因此它被永久更改!是这样说的。我有Python 3.3。5@Codo:您也可以而且应该在Python 2中使用此语法。在Python 2.+1中被列为不推荐使用的函数,用于显示如何通过在模块上使用
dir
自行调试此问题。是的,我使用的是3.3.5!
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']
>>> 'abcc'.count('c')
2
>>> str.count('abcc','c')
2