Andrzej Kolinski wrote: > > Let's forget for the time being my previous topic (files - strings - > lists). As a result of (not only) my efforts I arrived to the point that > I have data I need but I do not know how to process them. Form each > (game) file a dictionary is created containing player names (key) and > the scores + boards played (tuple), ex.: > from file 1 - > {'Chrabalowski': [(21.0, 24)], 'Stankiewicz': [(-28.0, 24)], ... more ...} > from file 2 - > {'Chrabalowski': [(45.0, 25)], 'Orlicka': [(-27.0, 25)], ... more ...} > from file 3 - > {'Chrabalowski': [(-23.0, 25)], 'Stankiewicz': [(20.0, 25)], ... more > ...}, etc. > > Eventually I will have 10 files to process. I need help how to calculate > a final ranking for each player using individual data. Many names will > be repeated but there will be some that appear only once or twice. So > for "Chrabalowski" we'll have: > (21.0 + 45.0 - 23.0)/(24 + 25 + 25), > and for "Stankiewicz: > (- 28.0 + 20.0)/(24 + 25)
I think you would be better off creating a single dictionary whose keys are the player names and values are a list of tuples of game data. In your example the dict would be {'Chrabalowski': [(21.0, 24),(45.0, 25),(-23.0, 25)], 'Stankiewicz': [(-28.0, 24)(20.0, 25)], 'Orlicka': [(-27.0, 25)], ... more ...} Then it will be much simpler to process each player because all his data will be in one place. There is a compact idiom for creating a dict where the values are accumulated in lists. Imagine you have a dict, key and value d, k, v and you want to append v to the list associated with k. A simple approach might be something like this: lst = d.get(k) if lst is None: lst = [] d[k] = lst lst.append(v) Note there is no need to assign d[k] = lst in the case where d[k] is already defined, because the list returned by get() is still in the dict and will be modified by the append(). (See the recent thread "How to Pass lists by value" if you don't understand this part.) dicts actually have a (confusingly named) method which already does most of the above - dict.setdefault(). Using setdefault() the above code can be written as lst = d.setdefault(k, []) lst.append(v) or even simpler: d.setdefault(k, []).append(v) Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor