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 8162503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:43:58+00:00 2026-06-06T18:43:58+00:00

Here’s what I want to write: groups[m][n] = groups[m – 1][n] or ++gid Here’s

  • 0

Here’s what I want to write:

groups[m][n] = groups[m - 1][n] or ++gid

Here’s what I have to write:

g = groups[m - 1][n]
if g:
    groups[m,n] = g
else:
    gid += 1
    groups[m][n] = gid

Is there no more compact way of writing that in Python simply because it lacks a ++ operator?


A larger sample from a method I’m working on:

groups = [[0] * self.columns] * self.rows
gid = 0
for m in xrange(self.rows):
    for n in xrange(self.columns):
        stone = self[m, n]
        if stone == self[m - 1, n]:
            if groups[m - 1][n]:
                groups[m][n] = groups[m - 1][n]
            else:
                gid += 1
                groups[m][n] = gid
        elif stone == self[m, n - 1]:
            if groups[m][n - 1]:
                groups[m][n] = groups[m][n - 1]
            else:
                gid += 1
                groups[m][n] = gid

I think it’s a lot harder to read when I have to blow it out like that, plus I’m evaluating m-1 twice… I’m not sure how I can condense it though.


This is what I came up with:

I created a wrapper class around int:

class Int(object):
    def __init__(self, i):
        self.i = i

    def pre(self, a=1):
        self.i += a
        return Int(self.i)

    def post(self, a=1):
        cpy = Int(self.i)
        self.i += a
        return cpy

    def __repr__(self):
        return str(self.i)

    def __nonzero__(self):
        return self.i != 0

Which can be used like this:

def group_stones(self):
    groups = [[None for _ in xrange(self.cols)] for _ in xrange(self.rows)]
    gid = Int(0)
    for m in xrange(self.rows):
        for n in xrange(self.cols):
            stone = self[m, n]
            if stone == self[m - 1, n]:
                groups[m][n] = groups[m - 1][n] or gid.pre()
            elif stone == self[m, n - 1]:
                groups[m][n] = groups[m][n - 1] or gid.pre()
            else:
                groups[m][n] = gid.pre()

Much like I would do in other languages.

  • 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-06T18:44:01+00:00Added an answer on June 6, 2026 at 6:44 pm
    gid = [0] # list - mutable object
    
    def incremented(gid):
        gid[0] += 1
        return gid[0]
    
    groups[m][n] = groups[m - 1][n] or incremented(gid)
    

    You can add some “magic” to your Int class:

    class C(object):
        ...
        def __add__(self, other):
            self.i += other
            return self.__class__(self.i)
        def __radd__(self, other):
            cpy = self.__class__(self.i)
            self.i += other
            return cpy
    
    >>> print Int(2) + 1 # pre
    3
    >>> i = Int(2)
    >>> print 1 + i # post
    2
    >>> print i
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem that I am trying to solve. I have two folders
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code (Say we have a single button on the page that
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here the problem. I have an ItemsControl , and I want to show a
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here's the basic setup: I have a thin bar at the top of a

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.