python: Attributeerror: 'int' Object has no attribute 'Count'

Asked

Viewed 719 times

0

I have a code that is returning me the following error

AttributeError: 'int' object has no attribute 'count'...

I have a list of multiple numbers, and I need to know how many repeat numbers from 1 to 13. The code below is just a small example to then transpose all this into something bigger.

#!/usr/bin/env python3

number_sequence = [
    16, 19, 1, 3, 8, 11, 18, 21, 13, 9, 24, 5, 23, 10, 6, 22, 19, 25, 3,
    2, 21, 20, 23, 24, 18, 9, 4, 6, 8, 15, 16, 22, 5, 25, 11, 14, 9, 4,
    13, 17, 15, 3, 10, 8, 6, 10, 15, 19, 7, 9, 8, 20, 22, 16, 4, 18, 1,
    11, 5, 21, 19, 1, 25, 7, 20, 22, 13, 8, 3, 17, 2, 15, 14, 12, 18, 18,
    25, 12, 8, 20, 24, 22, 4, 11, 14, 3, 23, 15, 10, 21, 10, 1, 4, 25,
    19, 7, 22, 11, 9, 23, 17, 14, 13, 18, 3, 7, 6, 23, 14, 1, 19, 8, 5,
    11, 25, 9, 16, 4, 17, 21, 7, 20, 6, 5, 21, 1, 17, 12, 16, 24, 3,
    23, 22, 4, 18, 1, 12, 4, 18, 7, 20, 11, 10, 2, 25, 14, 21, 6, 19, 23
]

for n in range(1, 14):
    print(n.count(number_sequence))

I do not understand why this error since if I run only one occurrence at a time it works right without any error.

For example: print(sequence_numbers.count(5))

  • 4

    n.count(number_sequence), you reversed. The correct is: number_sequence.count(n)

  • Problem solved, thank you very much Anderson Carlos Woss... I’ll get good at it yet, rsrsr. The mistake was so silly that not you believe...

2 answers

1

You can solve by creating a variable that stores the amount of times the numbers from 1 to 13 appear.

number_sequence = [
    16, 19, 1, 3, 8, 11, 18, 21, 13, 9, 24, 5, 23, 10, 6, 22, 19, 25, 3,
    2, 21, 20, 23, 24, 18, 9, 4, 6, 8, 15, 16, 22, 5, 25, 11, 14, 9, 4,
    13, 17, 15, 3, 10, 8, 6, 10, 15, 19, 7, 9, 8, 20, 22, 16, 4, 18, 1,
    11, 5, 21, 19, 1, 25, 7, 20, 22, 13, 8, 3, 17, 2, 15, 14, 12, 18, 18,
    25, 12, 8, 20, 24, 22, 4, 11, 14, 3, 23, 15, 10, 21, 10, 1, 4, 25,
    19, 7, 22, 11, 9, 23, 17, 14, 13, 18, 3, 7, 6, 23, 14, 1, 19, 8, 5,
    11, 25, 9, 16, 4, 17, 21, 7, 20, 6, 5, 21, 1, 17, 12, 16, 24, 3,
    23, 22, 4, 18, 1, 12, 4, 18, 7, 20, 11, 10, 2, 25, 14, 21, 6, 19, 23
]
# Variavel contadora
count = 0
for n in number_sequence:
    #Ela é incrementada toda vez que que o numero dentro do laço for de 1 a 13
    if(n >= 1 and n <= 13): count += 1

print ("Os Números de 1 a 13 se apareceram", count , "vezes")
  • 1

    n >= 1 and n <= 13 could be rewritten as 1 <= n <= 13 to simplify

  • Thank you, I’m knowing the language now so I don’t know much about it.

  • Lucas Emanuel his code tbm is interesting, but the previous solution of Anderson Carlos Woss was the one that best served me (in fact it was just an inversion that I did in my code)because what I needed to do was to know the number of times each number repeated within the list, e.g.: number 8 repeats 7x, number 9 6x and so on. In the case of output it would be print('[{:0>2}] repeats: {} times' .format(n, number_sequence.Count(n))... I will not even print this output, I will use it in a secondary list to plot on a matplotlib graph

1

You reversed the Count method call.

You are calling the Count method from the integer:

n.count(number_sequence)

But the method count belongs to list and counts how many times a certain value appears in a list.

number_sequence.count(n)

The code would look like this:

number_sequence = [
    16, 19, 1, 3, 8, 11, 18, 21, 13, 9, 24, 5, 23, 10, 6, 22, 19, 25, 3,
    2, 21, 20, 23, 24, 18, 9, 4, 6, 8, 15, 16, 22, 5, 25, 11, 14, 9, 4,
    13, 17, 15, 3, 10, 8, 6, 10, 15, 19, 7, 9, 8, 20, 22, 16, 4, 18, 1,
    11, 5, 21, 19, 1, 25, 7, 20, 22, 13, 8, 3, 17, 2, 15, 14, 12, 18, 18,
    25, 12, 8, 20, 24, 22, 4, 11, 14, 3, 23, 15, 10, 21, 10, 1, 4, 25,
    19, 7, 22, 11, 9, 23, 17, 14, 13, 18, 3, 7, 6, 23, 14, 1, 19, 8, 5,
    11, 25, 9, 16, 4, 17, 21, 7, 20, 6, 5, 21, 1, 17, 12, 16, 24, 3,
    23, 22, 4, 18, 1, 12, 4, 18, 7, 20, 11, 10, 2, 25, 14, 21, 6, 19, 23
]


for n in range(1, 14):
    print 'O numéro %s se repete %s vezes.' % (n, number_sequence.count(n))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.