Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Diagonal grid products
#1
  #--Horizonal
  for i in range(len(pList)-3):
    prod = 1
    for x in range(4):
      prod *= int(pList[i+x])
    prods.append(prod)

  #--Vertical
  for i in range(114):
    prod = int(pList[i])
    step = 0
    thelist = str(prod) + ","
    for x in range(3):
      step += i + 20
      thelist += str(pList[step]) + ","
      prod *= int(pList[step])
    prods.append(prod)
So I have a 20x20 grid of numbers (it's actually just a list of numbers, and I'm parsing it like it's 20x20. I am able to get the horizonal and vertical products of every set of 4 numbers, but I can't quite wrap my head around getting the diagonal ones. I tried altering the code for vertical and just changing the 20 to 21, which does the job, but it also wraps around the grid (it grabs an item on position 17-20 of each line and goes diagonal, only to wrap into the next line, when it shouldn't grab anything at all passed element 16 of each line). Also, it doesn't grab the reverse.

I've looked online, can't really find anything that suits my needs to learn from. Any pointers here?

Thanks
Reply
#2
see: https://docs.scipy.org/doc/numpy-1.13.0/...gonal.html
if you have to do it manually, you can do:
starting from x and y at top left, decrement x and increment y proportionally to get top left to bottom right,
and starting from bottom left., increment x and y proportionally
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How can I save cartesian products to a dataframe? noahverner1995 1 1,609 Dec-27-2021, 09:15 AM
Last Post: noahverner1995
  numpy diagonal davidm 1 1,756 Aug-02-2020, 01:09 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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