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

  • SEARCH
  • Home
  • 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 8260455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:06:12+00:00 2026-06-08T03:06:12+00:00

I am trying to calculate the origin and offset of variable size arrays and

  • 0

I am trying to calculate the origin and offset of variable size arrays and store them in a dictionary. Here is the likely non-pythonic way that I am achieving this. I am not sure if I should be looking to use map, a lambda function, or list comprehensions to make the code more pythonic.

Essentially, I need to cut chunks of an array up based on the total size and store the xstart, ystart, x_number_of_rows_to_read, y_number_of_columns_to_read in a dictionary. The total size is variable. I can not load the entire array into memory and use numpy indexing or I definitely would. The origin and offset are used to get the array into numpy.

intervalx = xsize / xsegment #Get the size of the chunks
intervaly = ysize / ysegment #Get the size of the chunks

#Setup to segment the image storing the start values and key into a dictionary.
xstart = 0
ystart = 0
key = 0

d = defaultdict(list)

for y in xrange(0, ysize, intervaly):
    if y + (intervaly * 2) < ysize:
        numberofrows = intervaly
    else:
        numberofrows = ysize - y

    for x in xrange(0, xsize, intervalx):
        if x + (intervalx * 2) < xsize:
            numberofcolumns = intervalx

        else:
            numberofcolumns = xsize - x
        l = [x,y,numberofcolumns, numberofrows]
        d[key].append(l)
        key += 1
return d

I realize that xrange is not ideal for a port to 3.

  • 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-06-08T03:06:13+00:00Added an answer on June 8, 2026 at 3:06 am

    This code looks fine except for your use of defaultdict. A list seems like a much better data structure because:

    • Your keys are sequential
    • you are storing a list whose only element is another list in your dict.

    One thing you could do:

    • use the ternary operator (I’m not sure if this would be an improvement, but it would be fewer lines of code)

    Here’s a modified version of your code with my few suggestions.

    intervalx = xsize / xsegment #Get the size of the chunks
    intervaly = ysize / ysegment #Get the size of the chunks
    
    #Setup to segment the image storing the start values and key into a dictionary.
    xstart = 0
    ystart = 0
    
    output = []
    
    for y in xrange(0, ysize, intervaly):
        numberofrows = intervaly if y + (intervaly * 2) < ysize else ysize -y
        for x in xrange(0, xsize, intervalx):
            numberofcolumns = intervalx if x + (intervalx * 2) < xsize else xsize -x
            lst = [x, y, numberofcolumns, numberofrows]
            output.append(lst)
    
            #If it doesn't make any difference to your program, the above 2 lines could read:
            #tple = (x, y, numberofcolumns, numberofrows)
            #output.append(tple)
    
            #This will be slightly more efficient 
            #(tuple creation is faster than list creation)
            #and less memory hungry.  In other words, if it doesn't need to be a list due
            #to other constraints (e.g. you append to it later), you should make it a tuple.
    

    Now to get your data, you can do offset_list=output[5] instead of offset_list=d[5][0]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
Trying to calculate (a+b)^n where n is a real value in a BigDecimal variable,
I am trying to calculate an initial buffer size to use when decompressing data
I'm trying to calculate all the possible values of a grid size (x by
I am trying to calculate cumulative product for subsets of xts object. Here is
I'm trying to calculate the shift patterns of people who work here, subtracting the
Am trying to calculate the number of rows in a table depending on a
I am trying to calculate number of users, cumulatively for the dellstore2 database. Looking
I'm trying to calculate the time it takes to receive all data from a
I am trying to calculate a ranking of a team in an ordered MySQL

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.