Python Forum
Determining the Percentage of Voter Population
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determining the Percentage of Voter Population
#6
You can use the max function with lambda:
max(data, key=lambda vote: vote.voters / vote.population).name
Instead you could write it as a function, which is easier to unterstand.
def relative(country):
    return country.voters / country.population

max(data, key=relative).name
Just adding the calculated percentages to the class is a better solution.
from functools import total_ordering


@total_ordering
class County:
   
  def __init__(self, name, population, voters):
    self.name = name
    self.population = population
    self.voters = voters
    self.relative = voters / population
  def __lt__(self, other):
      return self.relative < other.relative
  def __gt__(self, other):
      return self.relative > other.relative
  def __eg__(self, other):
      return self.relative == other.relative
Now you can apply max on the data list. The function max returns the whole object from the list.
The total_ordering decorator modifies the class. It will create the missing comparisons like <= >= and !=.

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 = max(data)
print(result, type(result), result.name)
Output:
<__main__.County object at 0x7f6f47658610> <class '__main__.County'> chester
As you can see, the representation is missing. It could be added with the method __repr__, but it's not a must.
Dataclasses could do this automatically. Since Python 3.7 they are available.

from dataclasses import dataclass, field


@dataclass(order=True)
class County:
    name: str = field(compare=False)
    population: int = field(compare=False)
    voters: int = field(compare=False)
    relative: float = field(compare=True, default=0.0)
    def __post_init__(self):
        self.relative: float = self.voters / self.population
County('Test', 100, 1)
Output:
County(name='Test', population=100, voters=1, relative=0.01)
So it has also a nice presentation. In this case the relative value is set to 0.0.
After __post_init__ was called by dataclass itself,
the self.relative is overwritten with the calculated value.
You can also define how dataclasses are sorted.
For example you can make more then on field to compare, but you have to exclude other
fields, if they are not used for sorting. In this case relative is the only field to compare.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Determining the Percentage of Voter Population - by DeaD_EyE - Dec-29-2019, 09:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Linked List - Ordering by largest population with country name. PaleHorse 2 3,097 Jun-16-2020, 09:04 PM
Last Post: jefsummers
  finding percentage in a list. samh625 3 5,778 Jun-15-2020, 06:49 PM
Last Post: samh625
  Numerically determining the highest a ball travels when shot straight up JakeWitten 3 3,547 Apr-22-2017, 04:37 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020