-5

I have a dictionary with integer values as keys and the values have either one or multiple values as list.

    100 ['Roopa Valipe ']
    99 ['John Smith', 'Souju Goud']
    98 ['Hemanth Hegde']

I have to assign values and print the output as follows:

    PERSON          SCORE       POSITION
    Roopa Valipe    100         1
    John Smith      99          2
    Souju Goud      99          2
    Hemanth Hegde   98          4

Any guidance on this is very helpful.

Update: I have now reversed the values into keys and vice versa. I can read one single key value. But if I have multiple keys mapping into a single 'value', I am not quite sure how to proceed from here.

['Hemanth Hegde'] 98
['John Smith', 'Souju Goud'] 99
['Roopa Valipe '] 100

This is what I have now.

djpanda
  • 835
  • 10
  • 20

4 Answers4

1

Here's some code that should work for you.

data = {
    100: ['Roopa Valipe'],
    99:  ['John Smith', 'Souju Goud'],
    98: ['Hemanth Hegde'],
}

fmt = '{:<20}{:<10}{:<10}'
print fmt.format('PERSON', 'SCORE', 'POSTITION')
position = 1
for score, people in sorted(data.items(),reverse=True):
    for person in sorted(people):
        print fmt.format(person, score, position)
    position += len(people)

Output

PERSON              SCORE     POSTITION 
Roopa Valipe        100       1         
John Smith          99        2         
Souju Goud          99        2         
Hemanth Hegde       98        4         
mhawke
  • 84,695
  • 9
  • 117
  • 138
0

In Python you can use any hashable type as a dictionary key. Integers and strings are hashable. However, so are many immutable data structures like tuples.

To directly answer your question, simply use the integer as the key. Did you try doing this before?

justanr
  • 700
  • 7
  • 13
  • I have a very basic knowledge of python as I am still learning. I understand the concept of hashable types and using them as dictionary keys. I am just not able to figure out how to put this into use. – djpanda Sep 18 '14 at 12:41
0

Here is a quick solution. If I have more time later, I try to rewrite it in more basic and simplified way or add comments (but with a bit of patience and consulting docs, you may learn few things ;)

import ast

indata = """
    100 ['Roopa Valipe ']
    99 ['John Smith', 'Souju Goud']
    98 ['Hemanth Hegde']
"""

parts = (row.split(None, 1) for row in indata.splitlines())
not_empty = filter(bool, parts)

d = {}
for value, keys in not_empty:
    keys, value = map(ast.literal_eval, (keys, value))
    d.update({key : value for key in keys})
score_first = [(y, x) for x, y in d.items()]
result = sorted(score_first, reverse=True)

for place, (score, name) in enumerate(result, start=1):
    print "{name:<20}{score:>4}{place:>4}".format(**locals())
m.wasowski
  • 6,329
  • 1
  • 23
  • 30
0

As justanr pointed out integers can be used for dictionary keys. Like so:

d = {100: ['Roopa Valipe '],
    99: ['John Smith', 'Souju Goud'],
    98: ['Hemanth Hegde']
    }

To your second question: How to create this table? The biggest issue is that a dictionary is a data structure that does not keep the order of items and sorting the keys is a problem. See the post here. You can get around this by creating a list of keys. The list can than be sorted.

format_strg = "{0:20}{1:<10}{2:<10}\n"
out = format_strg.format("PERSON", "SCORE", "POSITION") 
keys = sorted(d.keys(), reverse=True) #create a list of sorted keys
for pos,key in enumerate(keys):
    for item in d[key]:
        out += format_strg.format(item, key, pos+1)
print out
Community
  • 1
  • 1
Roman
  • 156
  • 5