Python Forum

Full Version: Cycles homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# 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? Cry

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)
________________________________________
Wall
Error:
Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users//AppData/Roaming/JetBrains/PyCharmCE2021.2/scratches/temp2.py", line 11, in <module> a = january_price_list[i] TypeError: list indices must be integers or slices, not list
Huh
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
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 !!!!)

Dance Dance Dance Angel