![]() |
dictionaries - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: dictionaries (/thread-8772.html) |
dictionaries - ricardons - Mar-07-2018 Hi I'm struggling with this variable that i think is a dictionary This is what i have page = [ {'id': 1, 'name': 'initial', 'components': [ {'id': 1, 'type': 'button', 'name': 'bt_settings'}, {'id': 2, 'type': 'button', 'name': 'bt_sensors'}, {'id': 3, 'type': 'button', 'name': 'bt_start'}, ] }, {'id': 2, 'name': 'settings', 'components': [ {'id': 1, 'type': 'text', 'name': 'txt_filename'}, {'id': 2, 'type': 'text', 'name': 'txt_googlecrd'}, ] }, {'id': 3, 'name': 'testType', 'components': [ {'id': 11, 'type': 'button', 'name': ''}, {'id': 12, 'type': 'text', 'name': 'txt_other'}, ] }, ]Now let's suppose that for the component 'txt_filename' in page 'settings' i want to know the respective id fields? In this case page id = 2 and component id = 1 I'm very confused. Should this be a class? how do i do it? help here RE: dictionaries - DeaD_EyE - Mar-07-2018 Use Python to inspect the object: dataset = page = [ {'id': 1, 'name': 'initial', 'components': [ {'id': 1, 'type': 'button', 'name': 'bt_settings'}, {'id': 2, 'type': 'button', 'name': 'bt_sensors'}, {'id': 3, 'type': 'button', 'name': 'bt_start'}, ] }, {'id': 2, 'name': 'settings', 'components': [ {'id': 1, 'type': 'text', 'name': 'txt_filename'}, {'id': 2, 'type': 'text', 'name': 'txt_googlecrd'}, ] }, {'id': 3, 'name': 'testType', 'components': [ {'id': 11, 'type': 'button', 'name': ''}, {'id': 12, 'type': 'text', 'name': 'txt_other'}, ] }, ] type(dataset) A list is a sequence of objects. Accessing the items is possible with iteration in a for-loop and/or using the index/slice access.Let's inspect the list: for element in dataset: print('Element', type(element), element)Now we see, that the list has only elements which are dicts. The next step should be to iterate over the list and compare the vlaue of the name with the string literal 'settings'. for element in dataset: if element['name'] == 'settings': print(element) Ok, we got the right 'row'. You need the page_id, which is in the dict and the component_id which is in the dicts, which are in a list.This will end in a nested loop: for element in dataset: if element['name'] == 'settings': page_id = element['id'] for component in element['components']: if component['name'] == 'txt_filename': component_id = component['id'] print('page_id:', page_id) print('component_id:', component_id) break The next step should be to make a function. The function should take as arguments the data and the name, which you are searching for. The function should return the both values page_id and component_id.
RE: dictionaries - ricardons - Mar-07-2018 thanks and if it is inside a class class NextionApp: def __init__(self): pages = [ {'id': '0', 'name': 'credits', 'components': [ {'id': '0', 'type': 'button', 'name': 'b0'}, ] }, {'id': '1', 'name': 'initial', 'components': [ {'id': '1', 'type': 'button', 'name': 'bt_settings'}, {'id': '2', 'type': 'button', 'name': 'bt_sensors'}, {'id': '3', 'type': 'button', 'name': 'bt_start'}, ] },how that would it change the call of the search? (Mar-07-2018, 11:07 AM)ricardons Wrote: thanks sorry about the mess in the messages. consider only the last reply RE: dictionaries - DeaD_EyE - Mar-07-2018 I think it's the wrong way. Think about how this class should work. You have some input, in this case this datastructure in python code. Then you have some attributes. What should you store inside the class? A simple function is the first step. Before you think about classes, do it as a function. def get_page_component(dataset): for element in dataset: if element['name'] == 'settings': page_id = element['id'] for component in element['components']: if component['name'] == 'txt_filename': component_id = component['id'] # leaving the loop here return page_id, component_id page_id, component_id = get_page_component(dataset)But what are you really trying? Storing this data into a class hardcoded is maybe not what you want. This dataset is coming from somewhere or it's just an example to learn? By the way, you assign to a local variable in the __init__ method. Local context inside a function is destroyed when leaving the function. You can assign pages to a class attribute. In this case it's available and shared across all instances of the class. If you want to store an attribute in a method call of a class, use self.pages = ... .For me it's too late. Too tired. You should read more examples. It's normally bad to come up with a solution. I learned a lot from mistakes I made. RE: dictionaries - wavic - Mar-07-2018 Looks like JSON to me. RE: dictionaries - Kennedy - Feb-11-2019 look very cool |