Problem Description
I am firstly I am having a list of values like:
(1,2,3,1,3,2,2,2,1)
What I want to achieve is :
- Find the most common one (working).
- Delete it from the list (working).
- Repeat the same thing for the remaining values in the list.
For instance
Taking this Tuple:
(1,2,3,1,3,2,2,2,1)
Store that 2 is the most common int, then
(1,3,3,1,1,)
Store that 1 is the second most common int, then
(3,3)
Store that 2 is the third most common int, then
My source Code
NOTE lets say that self.final_votes
would be (1,2,3,1,3,2,2,2,1)
def erase_common(self):
possitions = []
counter = 0
common = self.final_votes[0]
for i in self.final_votes:
cand_freq = self.final_votes.count(i)
if(cand_freq> counter):
counter = cand_freq
common = i
possitions.append(common)
while common in self.final_votes:
self.final_votes.remove(common)
this is the whole code
class voting_system:
def __init__(self):
#self.tutor_group = self.get_tutor_group()
self.stud_num = self.get_stud_num()
self.cand_num = self.get_cand_num()
self.cand_name = self.get_cand_name()
self.final_votes = self.ind_votes()
self.erase_common()
# def get_tutor_group(self):
# tutor_groups = ("7A","7B","7C","7D","7E","7F","8A","8B","8C","8D","8E","8F","9A","9B","9C","9D","9E","9E",
# "9F","10A","10B","10C","10D","10E","10F","11A","11B","11C","11D","11E","11F")
#
# flag = True
# while flag == True:
# try:
# self.tutor_group = input("please enter the tutor group: ")
# if self.tutor_group not in tutor_groups:
# raise ValueError("Tutor group entred doesn't exist")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
# return self.tutor_group
def get_stud_num(self):
# flag = True
# while flag == True:
# try:
# self.stud_num = int(input("Please enter number of students: "))
# if self.stud_num<0 or self.stud_num>35:
# raise ValueError("NUMBER OF STUDENTS INVALID")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
self.stud_num = 7
return self.stud_num
def get_cand_num(self):
# flag = True
# while flag == True:
# try:
# self.cand_num = int(input("Please enter number of candidates: "))
# if self.cand_num>4:
# raise ValueError("NUMBER OF CANDIDATES INVALID")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
self.cand_num = 3
return self.cand_num
def get_cand_name(self):
self.cand_name = []
for i in range(self.cand_num):
candidate = input("Enter candidate name: ")
self.cand_name.append(candidate)
return self.cand_name
def print_cands(self):
for i in range(len(self.cand_name)):
print(i+1, " : ", self.cand_name[i])
def ind_votes(self):
self.final_votes = []
self.print_cands()
abs_vote = 0
flag = True
while flag == True:
try:
for i in range(self.stud_num):
vote = input("To vote type 'V' and the candidates number, to abstent type 'A': ")
if vote.isalpha() == True:
abs_vote +=1
else:
self.final_votes.append(vote[1])
if vote[1] > str(self.cand_num):
raise ValueError("VOTED CANDIDATE NONEXISTENT")
except ValueError as error:
print(error)
flag = True
else:
flag = False
return self.final_votes
return abs_vote
def erase_common(self):
possitions = []
counter = 0
common = self.final_votes[0]
for i in self.final_votes:
cand_freq = self.final_votes.count(i)
if(cand_freq> counter):
counter = cand_freq
common = i
possitions.append(common)
while common in self.final_votes:
self.final_votes.remove(common)
run = voting_system()
any suggestions would be appreciated:)