Thursday, 15 July 2010

python - Pythonic alternative to (nested) dictionaries with the same keys? -



python - Pythonic alternative to (nested) dictionaries with the same keys? -

i find myself avoiding dictionaries because, often, half code duplicate. typically in nested dictionaries, sub-dictionaries contain same keys, different values.

i manually create big parent dictionary, each key contains nested dictionary, used in external modules. nested dictionaries utilize same keys define configuration parameters. usage explicit , works feels foolish retype or copy/paste keys each nested dictionary create manually. not overly concerned optimizing memory or performance, wondering if should doing another, more pythonic way.

as trivial illustration , pattern seen:

people_dict = { "charles lindberg": {"address": "123 st.", "famous": true}, "me": {"address": "456 st.", "famous": false} } >>>people_dict["charles lindberg"]["address"] "123 st."

while dictionary enables explicit code, tedious , error prone define nested dictionaries duplicate keys. in example, half nested dictionary code duplicate code mutual nested dictionaries. have tried using tuples eliminate duplicate keys find leads fragile code - alter in position (rather dictionary key) fails. leads code not explicit , hard follow.

people_dict = { "charles lindberg": ("123 st.", true), "me": ("456 st.", false), } >>>people_dict["charles lindberg"][0] "123 st."

instead, write class encapsulate same information: reduces duplicate code...

class person(object): def __init__(self, address, famous=false): self.address = address self.famous = famous people_dict = [ "charles lindberg": person("123 st.", famous=false), "me": person("456 st."), ] >>>people_dict["charles lindberg"].address "123 st."

creating class seems little overkill... standard info types seem basic...

i imagine there's improve way in python, without having write own class? what best way avoid duplicate code when creating nested dictionaries mutual keys? best answer: namedtuple

namedtuples solution; purpose, far lighter , point defining class. offer built in methods back upwards tuples, take default , optional args, , can passed ordereddict.

from collections import namedtuple person = namedtuple("person", ["name", "address", "famous"]) people_dict = { "charles lindberg": person("123 st.", true), "me": person("456 st.", false), } >>>people_dict["charles lindberg"].address "123 st."

it sounds have matrix of data, since every "row" has same keys (columns), i'd utilize numpy array:

import numpy np dtype = [('name', object), ('address', object), ('famous', bool)] people = np.array([ ("charles lindberg", "123 st.", true), ("me", "456 st.", false), ], dtype) charlie = people[people['name'] == 'charles lindberg'][0] print charlie['address']

or using pandas more high-level:

import pandas pd people = pd.dataframe(people_dict) print people['charles lindberg']['address']

that loads original dict-of-dicts people_dict straight matrix easily, , gives similar lookup features.

python class dictionary namedtuple

No comments:

Post a Comment