Quick Lists Question (Count Occurences) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Quick Lists Question (Count Occurences) (/thread-948.html) Pages:
1
2
|
Quick Lists Question (Count Occurences) - EwH006 - Nov-16-2016 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! RE: Quick Lists Question (Count Occurences) - Larz60+ - Nov-16-2016 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 againDon't put that in a loop RE: Quick Lists Question (Count Occurences) - EwH006 - Nov-16-2016 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. RE: Quick Lists Question (Count Occurences) - metulburr - Nov-16-2016 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. RE: Quick Lists Question (Count Occurences) - EwH006 - Nov-16-2016 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()) RE: Quick Lists Question (Count Occurences) - ichabod801 - Nov-16-2016 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. RE: Quick Lists Question (Count Occurences) - heiner55 - Nov-16-2016 Some tiny changes: from collections import Counter lst = [] print("Enter 9 numbers: ") for i in range(9): lst.append(int(input())) print(Counter(lst)) RE: Quick Lists Question (Count Occurences) - metulburr - Nov-16-2016 Quote:done it one way but the tool our class uses called 'Livelab' doesn't accept the codeI thought you couldnt use collections? RE: Quick Lists Question (Count Occurences) - heiner55 - Nov-16-2016 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)) RE: Quick Lists Question (Count Occurences) - nilamo - Nov-16-2016 (Nov-16-2016, 05:04 PM)metulburr Wrote: I thought you couldnt use collections?That's not OP. |