-2

Possible Duplicate:
changing the output

This is the code:

def voting_borda(args):
    results = {}
    for sublist in args:
        for i in range(0, 3):
            if sublist[i] in results:
                results[sublist[i]] += 3-i
            else:
                results[sublist[i]] = 3-i

    winner = max(results, key=results.get)
    return winner, results

print(voting_borda(
    ['GREEN','NDP', 'LIBERAL', 'CPC'],
    ['GREEN','CPC','LIBERAL','NDP'],
    ['LIBERAL','NDP', 'CPC', 'GREEN']
))

The output produced is

"('GREEN', {'LIBERAL': 5, 'NDP': 4, 'GREEN': 6, 'CPC': 3})"

I don't want the party names in the output (liberal, ndp, green and cpc) I just need the values, How can I edit the code to achieve that?

edit:

the error message i got after testing the above code (with: >>>voting_borda([['NDP', 'CPC', 'GREEN', 'LIBERAL'],['NDP', 'CPC', 'LIBERAL', 'GREEN'],['NDP', 'CPC', 'GREEN', 'LIBERAL']])

Traceback (most recent call last): File "", line 1, in voting_borda([['NDP', 'CPC', 'GREEN', 'LIBERAL'],['NDP', 'CPC', 'LIBERAL', 'GREEN'],['NDP', 'CPC', 'GREEN', 'LIBERAL']]) File "C:\Users\mycomp\Desktop\work\voting_systems.py", line 144, in voting_borda winner = max(results, key=results.get) NameError: global name 'results' is not defined

Community
  • 1
  • 1

2 Answers2

1

For Python 2.7 :

return winner, [value for value in results.values()])

For Python 3.x :

return winner, list(results.values())
asheeshr
  • 4,088
  • 6
  • 31
  • 50
0

Very old fashioned Python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

myResults=(['GREEN','NDP', 'LIBERAL', 'CPC'],
    ['GREEN','CPC','LIBERAL','NDP'],
    ['LIBERAL','NDP', 'CPC', 'GREEN'])

def count(results):
  counter = dict()
  for resultList in results:
    for result in resultList:
      if not(result in counter):
        counter[result] = 1
      else:
        counter[result] += 1
  print "counter (before): %s" % counter
  return counter.values()


if __name__ == "__main__":
  print "%s" % count(myResults)

If you're using Python >= 2.7, check the "collections.Counter" (as explained in this question)

Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136