Cycles homework - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Cycles homework (/thread-34857.html) |
Cycles homework - Roman_Y - Sep-08-2021 # Dear Python experts please help me with the task. _______________________________________________________________ delta_history = [ ['apple', ['january_price_delta', 'february_price_delta', 'march_price_delta']], ['orange', ['january_price_delta', 'february_price_delta', 'march_price_delta']] ] january_price_list = [{'product': 'apple', 'price': '100'}, {'product': 'orange', 'price': '200'}] february_price_list = [{'product': 'apple', 'price': '105'}, {'product': 'orange', 'price': '210'}] march_price_list = [{'product': 'apple', 'price': '110'}, {'product': 'orange', 'price': '215'}]_______________________________________________________________ # There're four lines: # delta_history containing 'product names', 'prices delta'(for example: price from Feruary minus price from January = february_price_delta) # january_price_list containing January products prices in JSON format # february_price_list containing February products prices in JSON format # march_price_list containing March products prices in JSON format # We have to count the deltas between the prices and put them into delta_history using Python with no importing # external modules. In case of error (no December prices) we have to see "No Data". # Important prerequisite: the code should work for any quantity of items in. # Important note: the number of delta_history list will come from the quantity of indexes in prices. # How to resolve the task using cycles? I broke at the beginning on: for i in delta_history: a = january_price_list[i] # How to put here a reference to the same index in january_price_list as the "i" is. print(a)________________________________________
RE: Cycles homework - Roman_Y - Sep-08-2021 RE: Cycles homework - jefsummers - Sep-08-2021 So delta_history is a list of lists of lists. The error appears to be when you are trying to reference a list, january_price_list[i] but i is a list, and therefore cannot be the index RE: Cycles homework - Roman_Y - Sep-09-2021 Thank you friends! I found my gap! for i, b in zip(march_price_list, february_price_list): d = int(i['price'])-int(b['price']) print(d)The difficulty was I thought to do the cycle with not approptiate list. And the second - I didn't know that it's possible to work with two lists by one cycle(and there is a damn wonderful thing for that called zip !!!!) |