Posts: 20
Threads: 4
Joined: Sep 2023
Sep-11-2023, 01:58 PM
(This post was last modified: Sep-11-2023, 01:58 PM by PythonBoy.)
I am currently making a program to find the Mode of a list by count() function.
Input =input("Please enter the data: ")
Data = Input.split(",")
#Storing all integers in this empty list
Data1 = []
#Converting all elements in list to integers
for x in Data:
Data1.append(int(x))
#Arranging the list in ascending order
Data1.sort(reverse=False)
print(Data1)
#Getting the length of the list
Length = len(Data1)
#Finding Mode
for l in range(Length):
Occurrence = Data1.count(Data1[l]) I figured out how to find the occurrence of each element of a list.
But I don't know how to compare them since all of the occurrences of each element will be stored in a single variable 'Occurence'.
Please help me with this project.
Thank you!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-11-2023, 02:43 PM
(This post was last modified: Sep-11-2023, 02:43 PM by snippsat.)
To fix some stuff and make it look better.
Try not to use capital letters for variables.
On line 2 make a list,so can use that list for rest of stuff to,no need to append to new list.
input_data = '1,2,3,1,2,2' #input("Please enter the data: ")
data = [int(i) for i in input_data.split(",")]
revers_data = reversed(data)
len_data = len(data) Test.
>>> data
[1, 2, 3, 1, 2, 2]
>>> revers_data
<list_reverseiterator object at 0x000002D212ABD360>
>>> list(revers_data)
[2, 2, 1, 3, 2, 1]
>>> len_data
6
>>> # Count a value in list
>>> data.count(2)
3 To count all occurrences in data list,can do it like this.
>>> from collections import Counter
>>>
>>> count_data = Counter(data)
>>> count_data
Counter({2: 3, 1: 2, 3: 1})
>>> count_data.most_common(1)
[(2, 3)]
>>> count_data.most_common(2)
[(2, 3), (1, 2)] So when do this most_common(1) it will find only the most common which is 2 that occur 3 times in data list.
Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-11-2023, 02:43 PM)snippsat Wrote: To fix some stuff and make it look better.
Try not to use capital letters for variables.
On line 2 make a list,so can use that list for rest of stuff to,no need to append to new list.
input_data = '1,2,3,1,2,2' #input("Please enter the data: ")
data = [int(i) for i in input_data.split(",")]
revers_data = reversed(data)
len_data = len(data) Test.
>>> data
[1, 2, 3, 1, 2, 2]
>>> revers_data
<list_reverseiterator object at 0x000002D212ABD360>
>>> list(revers_data)
[2, 2, 1, 3, 2, 1]
>>> len_data
6
>>> # Count a value in list
>>> data.count(2)
3 To count all occurrences in data list,can do it like this.
>>> from collections import Counter
>>>
>>> count_data = Counter(data)
>>> count_data
Counter({2: 3, 1: 2, 3: 1})
>>> count_data.most_common(1)
[(2, 3)]
>>> count_data.most_common(2)
[(2, 3), (1, 2)] So when do this most_common(1) it will find only the most common which is 2 that occur 3 times in data list.
Oh ok this is how you do it. I had no idea about Counter()
Thanks for letting me know. I will use it!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Posts: 6,824
Threads: 20
Joined: Feb 2020
Why are you converting the values to int? You aren't doing any arithmetic.
A way to do this without using any imports.
def mode(items):
counts = {}
for item in items:
counts[item] = counts.get(item, 0) + 1
return max(counts.items(), key=lambda x: x[1])[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(","))) Using a defaultdict.
from collections import defaultdict
def mode(items):
counts = defaultdict(lambda: 0)
for item in items:
counts[item] += 1
return max(counts.items(), key=lambda x: x[1])[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(","))) And using Counter.
from collections import Counter
def mode(items):
return Counter(items).most_common[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(",")))
Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-11-2023, 03:30 PM)deanhystad Wrote: Why are you converting the values to int? You aren't doing any arithmetic.
A way to do this without using any imports.
def mode(items):
counts = {}
for item in items:
counts[item] = counts.get(item, 0) + 1
return max(counts.items(), key=lambda x: x[1])[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(","))) Using a defaultdict.
from collections import defaultdict
def mode(items):
counts = defaultdict(lambda: 0)
for item in items:
counts[item] += 1
return max(counts.items(), key=lambda x: x[1])[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(","))) And using Counter.
from collections import Counter
def mode(items):
return Counter(items).most_common[0]
while items := input("Enter values separated by comma: "):
print("Mode = ", mode(item.strip() for item in items.split(",")))
No actually it is required, since I will be finding Mean, Median and Mode.
I didn't want to put the whole code which includes Mean and Median.
So I converted all the elements in the list from string to integer
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Posts: 6,824
Threads: 20
Joined: Feb 2020
Sep-11-2023, 04:13 PM
(This post was last modified: Sep-11-2023, 04:15 PM by deanhystad.)
If you want to convert values to int then compute the mode fine. If you pass strings and convert the result to an int, fine. Mode doesn't care. If you want to convert the inputs to floats, oh, I guess mode would care.
You should write you code using functions.
You might want to learn about list comprehensions. This:
Input =input("Please enter the data: ")
Data = Input.split(",")
#Storing all integers in this empty list
Data1 = []
#Converting all elements in list to integers
for x in Data:
Data1.append(int(x)) Becomes:
data = [int(x) for x in input("Please enter the data: ").split(",")] Python naming convention does not use uppercase letters for variables UNLESS the variable is global, then the name should be all uppercase. Since you are just starting, might as well start using the conventions everyone else uses (PEP8).
Posts: 20
Threads: 4
Joined: Sep 2023
(Sep-11-2023, 04:13 PM)deanhystad Wrote: If you want to convert values to int then compute the mode fine. If you pass strings and convert the result to an int, fine. Mode doesn't care. If you want to convert the inputs to floats, oh, I guess mode would care.
You should write you code using functions.
You might want to learn about list comprehensions. This:
Input =input("Please enter the data: ")
Data = Input.split(",")
#Storing all integers in this empty list
Data1 = []
#Converting all elements in list to integers
for x in Data:
Data1.append(int(x)) Becomes:
data = [int(x) for x in input("Please enter the data: ").split(",")] Python naming convention does not use uppercase letters for variables UNLESS the variable is global, then the name should be all uppercase. Since you are just starting, might as well start using the conventions everyone else uses (PEP8).
Ohh, I didn't know that such a big code can be converted into just one line. I didn't know because I have only learnt about variables, operations, lists, tuples and loops. Now that I know, I will make sure to use them.
I even didn't know we shouldn't use Capital letter for Variables. I will make sure to write it all in lower case from next time
Thanks!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
|