Issues with references - python

I am trying to calculate the voter turnout by dividing the number of votes by the population for a few Counties. The script should then determine and return the county with the highest turnout.
I am having trouble with my order of operations and am not sure where I am going wrong, I seem to have issues if class and my def highest_turnout(data) are not on the first space but that space keeps throwing an error for my line "results" saying "highest_turnout(data) is not defined" but if I indent def highest_turnout then I get NameError "highest_turnout" is not defined.... I understand why I am getting the name error because that definition is under class when it is indented -- I just dont know how to associate with the class and get the definition to run.
class County:# implement County class here
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters
self.turnout = []
#calculating turnout percentage
def add_turnout(self, turnout):
turnout = (self.voters / self.population)
if turnout not in self.turnout:
self.turnout.append(turnout)
return (turnout)
def highest_turnout(data) :
highest_turnout = data[0]
global turnout
if turnout > highest_turnout:
turnout = County.turnout
highest_turnout = County
return (highest_turnout, turnout)
# your program will be evaluated using these objects
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
result = highest_turnout(data) # do not change this line!
print(result) # prints the output of the function
# do not remove this line!

Looks like there's a few issues with your code and could probably be simplified quite a bit. If you're trying to find which County has the highest turnout you can do something like this:
class County:
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters
self.turnout = self.voters / self.population
def highest_turnout(county_list):
highest_turnout = 0
highest_county = None
for county in county_list:
if county.turnout > highest_turnout:
highest_turnout = county.turnout
highest_county = county.name
return(highest_county, highest_turnout)
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
result = highest_turnout(data)
print(result)

I feel like the function highest_turnout requires a loop. You're sending it a list of counties, and you should loop through them by doing something like
def highest_turnout(data):
highest_turnout = 0
for county in data:
county_turnout = county.turnout
if county_turnout > highest_turnout:
highest_turnout = county_turnout
highest_county = county
return highest_turnout, highest_county
This also requires you to simplify and rename the class method "add_turnout" to just "turnout". I dont know why you're trying to add a turnout, if it is just supposed to calculate the the voter turnout by dividing the voters through the population and return a percentage.
as a sidenote, I feel like making classes for this problem is very long-winded. You can achieve the same thing much easier using dictionaries instead. you should also rename "data" to "county_list" because this helps you understand whats in the data

Related

How do I return or print an attribute that's a math function between two attributes? Python

I'm very new to Python and have checked the three other posts about this subject but haven't been able to implement them successfully.
Essentially, I'm trying to return the name of the county with the highest voter turnout and the percentage. I can't seem to figure out how to return or print the latter part, as I don't have an attribute for the math portion (voters / population).
I've played around with some things like:
def percentage(self, turnout):
self.turnout = voters / population
Sorry if this post does not format correctly — this is all very new! Thanks in advance.
class County:
def __init__(self, name, population, voters):
self.name = name
self.population = population
self.voters = voters
def highest_turnout(data):
highest_county = data[0]
highest_percentage = (data[0].voters / data[0].population)
for county in data:
if (county.voters / county.population) > highest_percentage:
highest_county = county
highest_percentage = (county.voters / county.population)
return highest_county.name
# implement the function here
# your program will be evaluated using these objects
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469) # this is an object
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
result = highest_turnout(data) # do not change this line!
print(result) # prints the output of the function
# do not remove this line!
You simply return multiple values:
return highest_county.name, highest_percentage
In your calling program:
best_county, best_pct = highest_turnout(data)
What's very cool in python is you can actually just write the following:
highest_turnout = max(data, key=lambda county: county.voters / county.population)
Here, highest_turnout is the county with the highest turnout. What we've done is told python to calculate the maximum of the dataset where the values being compared is voters/population ie: the percentage of voters that came. In other words, this does exactly what your highest_turnout function does in a single line. You might consider defining a method for your County class called get_turnout() which just returns the percentage of the population that voted.
Obviously with highest_turnout we can write
highest_turnout.name
and
highest_turnout.voters / highest_turnout.population
to have the values you seek.

Continued Difficulties with Python Question

I asked this question earlier and am still having difficulties. I tried a new approach that isn't working. Essentially, I'm trying to implement a program that performs a calculation using Python objects to represent data. I want to determine the name of the county that had the highest voter turnout in a previous election, as well as the percentage of the population who voted. I need to use two function names, but can manipulate them however I see fit. Here's what I currently have and not sure what mistake I'm making here:
#creating a dictionary to store the country name and its percentage
data = {}
#creating the class county
class County:
def __init__(self,county,population,voters):
self.country = country
self.voters = voters
self.population = population
self.sorted_data = ""
self.formatted_percentage = ""
def highest_turnout(data) :
highest = data[0]
highest_percent = (data[0].voters / data[0].population)
for data in County
if (County.voters / County.population) > highest_percent
highest = County
highest_percent = County.data
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
I need the “highest_turnout” function to do this:
Find the County that has the highest turnout, i.e. the highest percentage of the
population who voted, using the objects’ population and voters attributes
Return a tuple containing the name of the County with the highest turnout and the
percentage of the population who voted, in that order; the percentage should be
represented as a number between 0 and 1
Display the results of any “print” functions, as well as the last one which prints the return value of the function. Note that your highest_turnout function should correctly determine the County with the highest turnout for any input list
Any explanations / advice on how to approach this would be greatly appreciated. Thank you as I'm pretty new to Python and want to learn as much as possible.
#creating a list to store the country name and its percentage
data = []
#creating the class county
class County:
def __init__(self,county,population,voters):
self.county = county
self.voters = voters
self.population = population
self.sorted_data = ""
self.formatted_percentage = ""
def highest_turnout(data) :
sorted_data_by_turnout = sorted(data, key=lambda county: county.voters / county.population, reverse=True)
highest_turnout_county = sorted_data_by_turnout[0]
return highest_turnout_county.county, (highest_turnout_county.voters / highest_turnout_county.population)
data = []
data.append(County("allegheny", 1000490, 645469))
data.append(County("philadelphia", 1134081, 539069))
data.append(County("montgomery", 568952, 399591))
data.append(County("lancaster", 345367, 230278))
data.append(County("delaware", 414031, 284538))
data.append(County("chester", 319919, 230823))
data.append(County("bucks", 444149, 319816))
print(highest_turnout(data))
FYI: there were some indentation errors in your code

Search a series for a word. Return that word and N others in a new column?

Okay, I need help. I created a function to search a string for a specific word. If the function finds the search_word it will return the word the and N words that precede it. The function works fine with my test strings but I cannot figure out how to apply the function to an entire series?
My goal is to create a new column in the data frame that contains the n_words_prior whenever the search_word exists.
n_words_prior = []
test = "New School District, Dale County"
def n_before_string(string, search_word, N):
global n_words_prior
n_words_prior = []
found_word = string.find(search_word)
if found_word == -1: return ""
sentence= string[0:found_word]
n_words_prior = sentence.split()[N:]
n_words_prior.append(search_word)
return n_words_prior
The current dataframe looks like this:
data = [['Alabama', 'New School District, Dale County'],
['Alaska', 'Matanuska-Susitna Borough'],
['Arizona', 'Pima County - Tuscon Unified School District']]
df = pd.DataFrame(data, columns = ['State', 'Place'])
The improved function would take the inputs 'Place','County',-1 and create the following result.
improved_function(column, search_word, N)
new_data = [['Alabama', 'New School District, Dale County','Dale County'],
['Alaska', 'Matanuska-Susitna Borough', ''],
['Arizona', 'Pima County - Tuscon Unified School District','Pima County']]
new_df = pd.DataFrame(new_data, columns = ['State', 'Place','Result'])
I thought embedding this function would help, but it has only made things more confusing.
def fast_add(place, search_word):
df[search_word] = df[Place].str.contains(search_word).apply(lambda search_word: 1 if search_word == True else 0)
def fun(sentence, search_word, n):
"""Return search_word and n preceding words from sentence."""
words = sentence.split()
for i,word in enumerate(words):
if word == search_word:
return ' '.join(words[i-n:i+1])
return ''
Example:
df['Result'] = df.Place.apply(lambda x: fun(x, 'County', 1))
Result:
State Place Result
0 Alabama New School District, Dale County Dale County
1 Alaska Matanuska-Susitna Borough
2 Arizona Pima County - Tuscon Unified School District Pima County

Python Class File to List

So I'm doing a project that I have to create a class that going to read information in a file like the one below, and put it in a list.
Each value in the list have to assign to a variable (name = 'Guillaume Dutroux')
Day: 2019-01-12
Time:
09:00
Company:
iCageDoree
Clients:
Guillaume Dutroux, london, 2019-03-12, 13:30, 55, 4*, plumbing, 4h00
Jose Quesada, madrid, 2019-03-12, 10:00, 30, 2*, refrigerators, 5h15
Martin Wyne, london, 2019-04-30, 19:45, 105, 3*, wifi, 0h30
class ReadClients:
def __init__(self, fileClients):
self._fileClients = fileClients
def readClients(self):
with open(self._fileClients, "r") as file:
for i in range(7):
file.readline()
self._clientsData = []
for line in file:
name, city, date, time, maxValue, minRep, domain, timeWork = line.strip(" ").split(",")
self._clientsData.append(Clients(name, city, date, time, maxValue, minRep, domain, timeWork))
self._nameCl = name
self._cityCl = city
self._dateCl = date
self._timeCl = time
self._maxValueCl = maxValue
self._minRepCl = minRep
self._domainCl = domain
self._timeWorkCl = timeWork
return self._clientsData
My code above is returning me:
[<clients.Clients object at 0x01B77170>, <clients.Clients object at 0x01B77230>, <clients.Clients object at 0x01B77310>]
and I don't know why.
Hope you can Help me.
Here is a small example of how you can play around with __repr__
class my_class:
def __init__(self,name,age):
self.name=name
self.age=age
def __repr__(self):
return "(name="+self.name+', '+"age"+str(self.age)+")"
my_list = [my_class('a',1),my_class('a',2)] # [(name=a, age1), (name=a, age2)]

Iterating over set of lists to find highest average in Python

How to create function which iterates over each county, calculating voter turnout percentage?
class County:
def __init__(self, init_name, init_population, init_voters) :
self.name = init_name
self.population = init_population
self.voters = init_voters
def highest_turnout(data) :
100 * (self.voters / self.population)
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
Your class County is defined correctly.
However, function county is not correct.
When passing data in the function highest_turnout, you have to first calculate the percentage of voters in the first County of the list - it is positioned at data[0].
Then we set “highest” to be the country name of the 1st County, we assume that the 1st in the data list is the highest one we have seen.
Next, we use a for loop to start iterating over all the County objects in the list data in order to pass in each County object.
The variable pct gives us the percentage of voters in the County that is running in the current step. The if function compares it to the highest percentage stored in the variable pct. If the new percentage is higher than pct (returns True), we update the highest percentage variable pct and hence update the county name.
def highest_turnout(data) :
highest_pct = data[0].voters / data[0].population
highest = data[0].name
for county in data :
pct = county.voters / county.population
if pct > highest_pct :
highest_pct = pct
highest = county.name

Categories

Resources