简体   繁体   中英

How may i find the key in my_dictionary , with dictionaries, and i don`t know exactly how many dictionaries i will have in my_dictionary?

How can I find the certain key in a dictionary, with dictionaries, and I don't know exactly how many dicts I have in a dict and depth of each dict. I need pass through this dict and find key - 'status'

I have this error:

RecursionError: maximum recursion depth exceeded in comparison

d = {'value': '1', 'unrestricted_value': '1', 'data': {'kpp': '550701001', 'capital': None, 'management': {'name': '3', 'post': 'd', 'disqualified': None}, 'founders': None, 'managers': None, 'predecessors': None, 'successors': None, 'branch_type': 'MAIN', 'branch_count': 0, 'source': None, 'qc': None, 'hid': '8e9c98d8d72c43551ae86fd61bc21dc088b81863b2b24ecb4a657d1c4ab72593', 'type': 'LEGAL', 'state': {'status': 'ACTIVE', 'code': None, 'actuality_date': 1560470400000, 'registration_date': 1243468800000, 'liquidation_date': None}, 'opf': {'type': '2014', 'code': '12300', 'full': 'j', 'short': 'ООО'}, 'name': {'full_with_opf': 'sbstbb', 'short_with_opf': 'y', 'latin': None, 'full': 'y', 'short': 'r'}, 'inn': '5505', 'ogrn': '109', 'okpo': '6136', 'okato': '52401364000', 'oktmo': '5270', 'okogu': '0014', 'okfs': '16', 'okved': '43.21', 'okveds': None, 'authorities': None, 'documents': None, 'licenses': None, 'finance': {'tax_system': None, 'income': None, 'expense': None, 'debt': None, 'penalty': None, 'year': None}, 'address': {'value': '5', 'unrestricted_value': '6', 'data': {'postal_code': '3', 'country': 't', 'country_iso_code': 'yu', 'federal_district': '3', 'region_fias_id': 'sdfg', 'region_kladr_id': '55000000', 'region_iso_code': 'nrn', 'region_with_type': 'Омская обл', 'region_type': 'обл', 'region_type_full': 'область', 'region': 'Омская', 'area_fias_id': None, 'area_kladr_id': None, 'area_with_type': None, 'area_type': None, 'area_type_full': None, 'area': None, 'city_fias_id': '27bf-4519-9ea0-6185d681d44e', 'city_kladr_id': '550000', 'city_with_type': 'г Омск', 'city_type': 'г', 'city_type_full': 'y', 'city': 'j', 'city_area': None, 'city_district_fias_id': None, 'city_district_kladr_id': None, 'city_district_with_type': 'g', 'city_district_type': 'округ', 'city_district_type_full': 'nh', 'city_district': 'b', 'settlement_fias_id': None, 'settlement_kladr_id': None, 'settlement_with_type': None, 'settlement_type': None, 'settlement_type_full': None, 'settlement': None, 'street_fias_id': '5043c277-0073-4000-b319', 'street_kladr_id': '55000001000005900', 'street_with_type': 'dsbesb', 'street_type': 'zvdf', 'street_type_full': 'бульвар', 'street': 'a', 'stead_fias_id': None, 'stead_cadnum': None, 'stead_type': None, 'stead_type_full': None, 'stead': None, 'house_fias_id': '39ab7e11-3980-46ba-816b-8f3e36dfad10', 'house_kladr_id': '5500000100000590021', 'house_cadnum': None, 'house_type': 'д', 'house_type_full': 'v', 'house': '3', 'block_type': 'к', 'block_type_full': 'корпус', 'block': '8', 'entrance': None, 'floor': None, 'flat_fias_id': None, 'flat_cadnum': None, 'flat_type': 'кв', 'flat_type_full': 'zdfbn', 'flat': '172/173', 'flat_area': None, 'square_meter_price': '54234', 'flat_price': None, 'postal_box': None, 'fias_id': '39ab7e11-3980-46ba-816b-8f3e36dfad10', 'fias_code': '55000001000000000590021', 'fias_level': '8', 'fias_actuality_state': '0', 'kladr_id': '5500000100000590021', 'geoname_id': '1496153', 'capital_marker': '2', 'okato': '52401364000', 'oktmo': '52701000001', 'tax_office': '5507', 'tax_office_legal': '5507', 'timezone': 'UTC+6', 'geo_lat': '54.991044', 'geo_lon': '73.3139787', 'beltway_hit': None, 'beltway_distance': None, 'metro': None, 'divisions': None, 'qc_geo': '1', 'qc_complete': None, 'qc_house': None, 'history_values': None, 'unparsed_parts': None, 'source': 'fg', 'qc': '0'}}, 'phones': None, 'emails': None, 'ogrn_date': 1243468800000, 'okved_type': '2014', 'employee_count': None}}

key = 'status'

def find_key(d, key):
    for i in dict:
        if i == key:
            print(i[key])
        elif i != key:
            return find_key(i, key)
print(find_key(d, 'status'))

Здравствуй, человек) Did not completely understand your question, but here is what I can offer:

key = "status"
def find_key(d, key):
    for d_key in d.keys():
        if d_key == key:
            print(d[key])
        else:
            if isinstance(d[d_key], dict):
                 find_key(d[d_key], key)
find_key(d, key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM