سؤال

I am viewing the Python tutorials from the Pascal institute BDFL says are the best to start and i have a very basic question

While in the tutorial says:

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

When i try to do is it does not work

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

Why this is happening?

I' ve searched Stack resoures BTW and I didn't find anything.

COMMENT

Take a look at the start of the section 1.1.3. You have to type first 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

I did.

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

I did. Didn' t work.

هل كانت مفيدة؟

المحلول 2

While the other answers are correct, current Python releases propose another way to call count, as it is usable for str but also any type of sequence, as advised inside the documentation:

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

As seq is as string object, it also have the count method.

نصائح أخرى

The tutorial you linked to is very old:

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

You're probably using a more modern Python (>= 3) in which case there are no longer string functions like count in the string module. We used to have

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

but today:

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

These days we use the string methods instead, the ones that live in str itself:

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

or even

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

This methodcount() is defined in string package. For using this method in your code, you need to import the definition. Adding the following import statement before using the method will solve your problem

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

count is a method in the string module, meaning that at the top of your file (before you use the function) you need to "import" it so that your interpreter knows what you're talking about. Add the line from string import count as the first line of your file and it should work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top