Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3789152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:06:40+00:00 2026-05-19T12:06:40+00:00

Sure, you can have nested lists to represent multidimensional arrays, but that seems costly…

  • 0

Sure, you can have nested lists to represent multidimensional arrays, but that seems costly…

[[0, 1], [2, 3]]

Is there some way to “encode” and “decode” the coordinate into a single number, and use that number to lookup the corresponding element?

[0, 1, 2, 3]

This needs to work with n-dimensions, not just two, and the best I could come up with for encoding is:

def getcellindex(self, location):
  cindex = 0
  cdrop = self.gridsize # where self.gridsize is the number of cells
  for index in xrange(self.numdimensions): # where self.numdimensions is the number of dimensions
    # where self.dimensions is a tuple of the different sizes of the corresponding dimension
    cdrop /= self.dimensions[index]
    cindex += cdrop * location[index]
  return cindex

There’re probably ways to optimize this, but more importantly, how do I reverse the process? And, does this function work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:06:40+00:00Added an answer on May 19, 2026 at 12:06 pm
    def getlocation(self, cellindex):
        res = []
        for size in reversed(self.dimensions):
            res.append(cellindex % size)
            cellindex /= size
        return res[::-1]
    

    Or, for the full test case

    class ndim:
        def __init__(self):
            self.dimensions=[8,9,10]
            self.numdimensions=3
            self.gridsize=8*9*10
    
        def getcellindex(self, location):
            cindex = 0
            cdrop = self.gridsize
            for index in xrange(self.numdimensions):
                cdrop /= self.dimensions[index]
                cindex += cdrop * location[index]
            return cindex
    
        def getlocation(self, cellindex):
            res = []
            for size in reversed(self.dimensions):
                res.append(cellindex % size)
                cellindex /= size
            return res[::-1]
    
    n=ndim()
    print n.getcellindex((0,0,0))
    print n.getcellindex((0,0,1))
    print n.getcellindex((0,1,0))
    print n.getcellindex((1,0,0))
    
    print n.getlocation(90)
    print n.getlocation(10)
    print n.getlocation(1)
    print n.getlocation(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is easy but I can't figure it out: I have an
I am building an application as a library, but to make sure I can
I need to make sure that user can run only one instance of my
I know how to use Perl's Getopt::Long, but I'm not sure how I can
I can't be sure if my code is sucking, or if it's just that
How can I make sure that a certain OLEDB driver is installed when I
I can get the element like this $(#txtEmail) but I'm not sure how to
I have a ListBox that can have multiple items selected at once. I have
I have some unusual I need to do. I am wondering if anyone can
I want to make sure people can't type the name of a PHP script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.