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 8885961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:26:27+00:00 2026-06-14T21:26:27+00:00

In the following code, why does my code not iterate properly? I’m probably missing

  • 0

In the following code, why does my code not iterate properly? I’m probably missing one line but I can’t figure out why it doesn’t work.

I have a function with the following test case:

>>> borda([['A', 'B', 'C', 'D'], ['B', 'A', 'C', 'D'], ['B', 'C', 'D', 'A']])
('B', [5, 8, 4, 1])

Where lists in the parameter are rankings, each #1 rank gets 3 points, #2 gets 2 points, #3 gets 1 point, and no other ranks get anything. There may not necessarily four choices. The first element in the tuple should be the choice with the highest number of points, and the second element is the number of points each choice got, in alphabetical order.

I’m not done with the function, but I’m trying to get a dictionary of the choices as the keys in alphabetical order and the count of rankings as the values, but the output is a dictionary of only the very last element of the last list in the parameter.

L = ['A', 'B', 'C', 'D'] #This is referenced outside the function since it might change
D = {}
i = 0
num = 0
while num < len(L):
    num += 1
    for choice in L:
        while i < len(parameter):
            for item in parameter:
                if item[0] == choice:
                    D[choice] = D.get(choice, 0) + 3
                if item[1] == choice:
                    D[choice] = D.get(choice, 0) + 2
                if item[2] == choice:
                    D[choice] = D.get(choice, 0) + 1
                i += 1
return D
  • 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-14T21:26:28+00:00Added an answer on June 14, 2026 at 9:26 pm

    The way I’d do this is something like this:

    import operator
    from collections import defaultdict
    listoflists = [['A', 'B', 'C', 'D'], ['B', 'A', 'C', 'D'], ['B', 'C', 'D', 'A']]
    
    def borda(listoflists):
       outdict = defaultdict(int)
       for item in listoflists:
          outdict[item[0]] += 3
          outdict[item[1]] += 2
          outdict[item[2]] += 1
    
       highestitem = max(outdict.iteritems(), key=operator.itemgetter(1))[0]
       outlist = [outdict[item[0]] for item in sorted(outdict.keys())]
    
       return (highestitem, outlist)
    

    Update:
    I’m not sure why you wouldn’t be able to import standard modules, but if for whatever reason you’re forbidden from using the import statement, here’s a version with only built-in functions:

    listoflists = [['A', 'B', 'C', 'D'], ['B', 'A', 'C', 'D'], ['B', 'C', 'D', 'A']]
    
    def borda(listoflists):
        outdict = {}
        for singlelist in listoflists:
            # Below, we're just turning singlelist around in order to
            # make use of index numbers from enumerate to add to the scores
            for index, item in enumerate(singlelist[2::-1]):
                if item not in outdict:
                    outdict[item] = index + 1
                else:
                    outdict[item] += index + 1
    
        highestitem = max(outdict.iteritems(), key=lambda i: i[1])[0]
        outlist = [outdict[item[0]] for item in sorted(outdict.keys())]
    
        return (highestitem, outlist)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following code: I can't figure out why j does not print on
the following code is very simple, but for some reason, for-loop does not iterate
The following code does not time out in Ruby 1.9.3p194 (2012-04-20) [i386-mingw32]: require 'timeout'
The following code does not want to compile. See the included error message. Code:
The following code does not run as rootNode is null when retrieved by name
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not give a warning with g++ 4.1.1 and -Wall .
The following code does not submit the input values: $('.button').live('click', function() { var date
The following code does not compile: //int a = ... int? b = (int?)

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.