Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41071

Re: working with dict : incrementing dict dynamically

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: working with dict : incrementing dict dynamically
Date 2013-03-11 15:37 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <khktna$g2r$1@reader2.panix.com> (permalink)
References <mailman.3191.1363015388.2939.python-list@python.org>

Show all headers | View raw


In <mailman.3191.1363015388.2939.python-list@python.org> inshu chauhan <insideshoes@gmail.com> writes:

> --14dae93408ffe4594104d7a7bf0c
> Content-Type: text/plain; charset=ISO-8859-1

> I am trying to create a dictionary with a key and its values seraching from
> a data set. But something is going wrong. This is the first time I am
> working with dicts.

> My code is :

> import cv
> def Computesegclass(segimage):
>     num_pixel = 0
>         for y in xrange(0, segimage.height):
>         for x in xrange(0, segimage.width):

>             if segimage[y,x] == (0.0, 0.0, 0.0):
>                 continue
>             else:
>                 color = segimage[y,x]
>                 blue = color[0]
>                 green = color[1]
>                 red = color[2]
>                 region_num = blue + 256 * green + 65536 * red
>                 print region_num
>                 segments = dict({region_num : num_pixel})

You're creating a new segments dictionary for each pixel, overwriting
the previous one.  Instead, you probably want to create an empty dictionary
at the top of your function.

>                 if region_num == region_num:

This if statement will always evaluate true, because you're comparing
region_num to itself.

>                     num_pixel = +1

This doesn't store the value of num_pixel in the dictionary.  You probably
want to do this instead:

    segments[region_num] += 1

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

working with dict : incrementing dict dynamically inshu chauhan <insideshoes@gmail.com> - 2013-03-11 16:23 +0100
  Re: working with dict : incrementing dict dynamically John Gordon <gordon@panix.com> - 2013-03-11 15:37 +0000

csiph-web