1

Basically my problem is this:

I have a list containing integers. In this case, the integers reflect greyscale values for individual pixels. I've established a variable (xpixel) that is the width of the photo in pixels. When my list hits this number (Just after the xpixel-1 pixel integer) i need to add a special identifying character to signal that the line of pixels has been completed and to begin the next line. This continues at intervals of xpixel through the entire integer string.

So, how do i add an item to my list at exactly that given interval?

Something like this?:

j=0
for i in Pixels:
    if j%xpixel==0 and j!=0:
        pixel_corrected.append(i)
        pixel_corrected.append('END PIXEL LINE')
        j+=1
    else:
        pixel_corrected.append(i)
        j+=1

Thanks in advance for the help.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Tsnorthern
  • 27
  • 5
  • 2
    Don't add an item which doesn't really belong to your list. Group your list into sublists instead (making a 2d matrix out of it). – sashkello Feb 21 '14 at 04:06

5 Answers5

2

Something like this?

>>> from itertools import chain
>>> xpixel = 5
>>> Pixels = range(12)
>>> list(chain.from_iterable([[item, 'END PIXEL LINE'] 
                 if j%xpixel==0 and j else [item] for j, item in enumerate(Pixels)]))
[0, 1, 2, 3, 4, 5, 'END PIXEL LINE', 6, 7, 8, 9, 10, 'END PIXEL LINE', 11]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Wow. This worked beautifully. Never used itertools before, clearly i need to give them a closer look. Thanks. – Tsnorthern Feb 21 '14 at 04:44
0

Well, your approach seems reasonable, we can make it more python-y though

for n, x in enumerate(Pixels):
    if n > 0 and n % xpixe == 0:
        # append your marker
    # append x

Alternatively, you can use an answer to this question and produce a list of lists.

Community
  • 1
  • 1
Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
0

Through list comprehension:

data = [1,2,3,4,5,6,7,8,9,10,11,12]
xpixel = 4
[x for l in [data[i:i+xpixel] + ['endl'] for i in range(len(data)/xpixel)] for x in l]

will produce

[1,2,3,4,'endl',5,6,7,8,'endl',9,10,11,12,'endl']
sashkello
  • 17,306
  • 24
  • 81
  • 109
0

You might consider using a list of lists to encode the photo, as mentioned by sashkello. Try a list comprehension:

>>> pixels = list(range(20))
>>> xpixel = 5
>>> [pixels[x:x+xpixel] for x in range(0, len(pixels), xpixel)]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]
butch
  • 2,178
  • 1
  • 17
  • 21
0

This sounds like a hack and you really need a simple object containing a list which provides a custom output method write_lines(linelength=...), which is a perfect use for itertools recipes.

This way we don't mess with the underlying list so you can still process it as a list.

import itertools

class PixelList(object):
    def __init__(self, lst, xpixel=None): # you could also parameterize fillvalue='END PIXEL LINE'
        self.lst = lst
        self.xpixel = xpixel
    def set_xpixel(self, xpixel):
        self.xpixel = xpixel
    #wrapperize whatever other methods of list you need to pass through
    #...
    def writelines(self, fillvalue='END PIXEL LINE'):
        # using itertools 'grouper' recipe: "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
        args = [iter(self.lst)] * self.xpixel
        print [a for a in args]
        return itertools.izip_longest(fillvalue=fillvalue, *args)

l = PixelList(range(1,54), 10)
smci
  • 32,567
  • 20
  • 113
  • 146