crossmax crossmax - 8 months ago 18
Python Question

How to find an item in a dict with nested classes?

I'm a newbie programming in python and I canĀ“t find a element in a complex dict (for me at least).

This dict contains items "FareAttribute" and the same time this class contains elements "FareRule". I want to find the element that matches FareRule.origin_id=="city1" and FareRule.destination_id=="city2".

How I can to find this?

Thanks for any comment in advance. I'm a bit lost

Edit to add dict (output when print first item). The classes belongs to transitfeed library (Google Transit). Right now I can't execute program, I'm out.

{u'AA': <FareAttribute [('currency_type', u'EUR'), ('fare_id', u'AA'), ('payment_method', 0), ('price', 1.5), ('rules', [<FareRule [('contains_id', None), ('destination_id', u'A'), ('fare_id', u'AA'), ('origin_id', u'A'), ('route_id', None)]>]), ('transfer_duration', None), ('transfers', 0)]>,...}

ccf ccf

EDIT2 Please try something like this (if python 2.7):

for fare in schedule.GetFareAttributeList():
    for rule in fare.GetFareRuleList():
        if rule.origin_id == 'B1' and rule.destination_id == 'B1':
            print rule