Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:06+00:00 2026-06-14T17:06:06+00:00

I’m new to programming and python is the first language I’ve learned. The question

  • 0

I’m new to programming and python is the first language I’ve learned.

The question I want to ask is how do you count the frequency of items in a list
so they add up in order with “PARTY_INDICES”? in my case that is.

This is a docstring for what I need to do:

''' (list of str) -> tuple of (str, list of int) 
votes is a list of single-candidate ballots for a single riding. 
Based on votes, return a tuple where the first element is the name of the party 
winning the seat and the second is a list with the total votes for each party in 
the order specified in PARTY_INDICES.

>>> voting_plurality(['GREEN', 'GREEN', 'NDP', 'GREEN', 'CPC']) 
('GREEN', [1, 3, 0, 1])
'''

Since PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]
This produces a tuple of the winning party (In this case ‘GREEN’) and the list of
frequencies, where [1, 3, 0, 1]

These are global variables, lists and dictionaries:

#  The indices where each party's data appears in a 4-element list.
NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3

# A list of the indices where each party's data appears in a 4-element list.
PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]

# A dict where each key is a party name and each value is that party's index.
NAME_TO_INDEX = {
  'NDP': NDP_INDEX,
  'GREEN': GREEN_INDEX,
  'LIBERAL': LIBERAL_INDEX,
  'CPC': CPC_INDEX
}

# A dict where each key is a party's index and each value is that party's name.
INDEX_TO_NAME = {
  NDP_INDEX: 'NDP',
  GREEN_INDEX: 'GREEN',
  LIBERAL_INDEX: 'LIBERAL',
  CPC_INDEX: 'CPC'
}

This is my work:

def voting_plurality(votes):
    my_list = []
    my_dct = {}
    counter = 0
    for ballot in votes:
        if (ballot in my_dct):
            my_dct[ballot] += 1
        else:
            my_dct[ballot] = 1

    if (my_dct):
        my_dct = my_dct.values()
        new_list = list(my_dct)

    return (max(set(votes), key = votes.count), new_list)

it returns:

>>> voting_plurality(['GREEN', 'GREEN', 'NDP', 'GREEN', 'CPC'])
('GREEN', [1, 1, 3])

But I want it to also include the party with no votes and is in order with PARTY_INDICES [1, 3, 0, 1]

My code may look like nonsense, but I’m really stuck and confused.

Also I cannot IMPORT anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:06:07+00:00Added an answer on June 14, 2026 at 5:06 pm

    There are two main problems you have. The first is that you have to capture the zero, but since there are no votes for the “liberal party”, the zero will not be reflected.

    Tip Maybe you want to initialize your dictionary?

    The second problem is that you are calling dict.values() which will not be in any sort of order. You need to use the dictionary, and the PARTY_INDICES to create the correctly ordered list of numbers.

    Tip Maybe you can reference the keys in the dictionary and their respective positions in the PARTY_INDICIES list

    See if you can come up with something given these tips, and update your question. If you can’t, I’m sure someone will post a full answer eventually.

    Seeing as it has been 4 hours – here is a solution:

    def voting_plurality(votes):
        sums = dict(zip(INDEX_TO_NAME.values(), [0] * len(INDEX_TO_NAME)))
        for vote in votes:
            if vote in sums:
                sums[vote] += 1
            else:
                print "Bad vote: %s" % vote
        votes_by_index = sorted([(NAME_TO_INDEX[k], v) for k, v in sums.items()])
        votes_by_rank = sorted(votes_by_index, key=lambda x: x[1], reverse=True)
        votes_by_parts = [item[1] for item in votes_by_index]
        highest_votes = INDEX_TO_NAME[votes_by_rank[0][0]]
        return (highest_votes, votes_by_parts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I would like to count the length of a string with PHP. The string
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.