We are running a survey to see what radish varieties
customers prefer the most. In this exercise, we will not only be finding the
maximum votes for the radish, but we will also play around with different
string functions in Python.
So first, we have created a text file name
"radishsurvey" with 300 lines of survey data. Each line consists of a
name, a hyphen, and then a radish variety. E.g. Angelina Belmore - Plum Purple
We would like to convert it into the form: Angelina Belmore voted for Plum Purple. Below
is a screenshot of the codes (highlighted) and the result in the console.
We would now like to see the list of people who like White Icicle. Below are the codes and result in the screenshot.
We would now like to see the list of people who like White Icicle. Below are the codes and result in the screenshot.
Let’s count the total number of votes for White Icicle. And
that’s 59!
Now,
we will try and count the votes for all the varieties. Below is a screenshot
with the codes and the result in console
Here, we learnt that our data has a lot of garbage values
and duplicates. We will now clean our data (also called data munging). We
convert the strings with double spaces between them by using: vote
= vote.replace(" ", "
"). We clean the vote string by using: vote = vote.strip().capitalize()
Strip() remove distinctions like " Cherry Belle"
and "Cherry Belle" and capitalize() is used to remove case
distinction, it makes the first letter capital.
We would also like to check if anyone voted twice.
There! 2 extra votes. Below is the screenshot
Now, let’s just print out the Winner’s name.







Screenshots have made it much more easier to understand the problem and solution.
ReplyDeleteEasy to understand because of structured approach.
ReplyDelete