Python Forum
Quick Lists Question (Count Occurences)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick Lists Question (Count Occurences)
#1
Good Evening,

I'm stuck in a headache and am looking to get some solutions. I've been given a problem that wants me to input 9 integers, and list the occurrences of those 9 integers. I've done it one way but the tool our class uses called 'Livelab' doesn't accept the code I gave it. For this purpose I will show both source code.


For the rejected code I used: 

from collections import Counter
a = [2, 5, 6, 5, 4, 3, 23, 43, 2]
print(Counter(a))
The code I'm working on currently looks like this. I've got some of the print statements to work within Idle, but am having trouble and the book I have has been little help.


for i in range(9):
   num_list = int(input("Enter integers between 1 and 100: "))
print(num_list.count(2))
print(num_list.count(5))
print(num_list.count(6))
print(num_list.count(4))
print(num_list.count(3))
print(num_list.count(23))
print(num_list.count(43))
Any help would be appreciated!
Reply
#2
On the numbers:
input all nine with one input statement:
my_numbers = input('Enter 9 numbers separated by spaces')
mn = my_numbers.split(' ')
if len(mn) <> 9):
    # Show error Do it again
Don't put that in a loop
Reply
#3
That isn't in python 3, or at least my interpret is saying it isn't. I'm talking about the <>.

I messed with that code a little bit though. That's not what I'm looking to do. I'm looking to input 9 numbers and after that count how many times each number occurs within that list.
Reply
#4
the easiest way to check occurrence if you cant use collections is to use a dictionary. The key as the identifyer (such as your 9 ints), and the value is the number of occurrences. Every time the dict key is inputed (one of your 9 ints), that key's value gets incremented.
Recommended Tutorials:
Reply
#5
I still am not getting where I need to be. The console is supposed to spit out how many times each integer I input occurs. I've altered my code a bit since last post. I'm working with what I had before now and this:

lst = []
print("Enter 9 numbers: ")
for i in range(9):
lst.append(eval(input()))
print(lst.count())
Reply
#6
Metulburr gave you the information you need to solve the problem. Don't use a list, use a dictionary. Have the key be the value inputted, and the value be the count. Increment the count each time you encounter a given input number. The only trick is to get the initial value for each number. You can do that with 'if key in dict:', although it is more common to use the get method of the dictionary.

And use code tags.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Some tiny changes:

from collections import Counter

lst = []
print("Enter 9 numbers: ")
for i in range(9):
    lst.append(int(input()))

print(Counter(lst))
Reply
#8
Quote:done it one way but the tool our class uses called 'Livelab' doesn't accept the code
from collections import Counter

a = [2, 5, 6, 5, 4, 3, 23, 43, 2]
print(Counter(a))
I thought you couldnt use collections?
Recommended Tutorials:
Reply
#9
This works also:

from collections import Counter
my_numbers = input('Enter 9 numbers separated by spaces: ')
lst = my_numbers.split(' ')
if len(lst) != 9:
    print("error: less or more than 9 numbers")

print(Counter(lst))
Reply
#10
(Nov-16-2016, 05:04 PM)metulburr Wrote: I thought you couldnt use collections?
That's not OP.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count occurences in a string and add to a list using loops Leyo 4 1,637 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Calculator I need quick help Malin3k 3 2,028 Feb-13-2021, 02:10 AM
Last Post: BashBedlam
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,065 Jun-19-2018, 02:52 AM
Last Post: bhill
  regex, counting occurences yanhto 4 3,311 May-06-2018, 10:20 PM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020