Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cycles homework
#1
# 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
Yoriz write Sep-08-2021, 10:59 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Homework and No Effort Questions
Reply
#2
Huh
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  strings that cycles lizarragaman 6 4,958 Feb-13-2017, 12:54 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020