non-copy slices

2009-11-18 Thread tbourden
Hi,

I was looking for a facility similar to slices in python library that would
avoid the implicit creation of a new list and copy of elements that is the
default behaviour. Instead I'd rather have a lazy iteratable object on the
original sequence. Well, in the end I wrote it myself but I was wondering if
I missed sth in the library. If I didn't is there a particular reason there
isn't sth like that? I find it hard to believe that all slice needs have
strictly copy semantics.

Cheers,
Themis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread tbourden
Ahhh yes! that's exactly it. Thanks for pointing out!

Themis


On Wed, Nov 18, 2009 at 3:44 PM, Tim Golden  wrote:

> [email protected] wrote:
> > Hi,
> >
> > I was looking for a facility similar to slices in python library that
> would
> > avoid the implicit creation of a new list and copy of elements that is
> the
> > default behaviour. Instead I'd rather have a lazy iteratable object on
> the
> > original sequence. Well, in the end I wrote it myself but I was wondering
> if
> > I missed sth in the library. If I didn't is there a particular reason
> there
> > isn't sth like that? I find it hard to believe that all slice needs have
> > strictly copy semantics.
>
> I suspect that itertools is your friend, specifically itertools.islice
>
> TJG
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread tbourden
Hi,

sth == something :) sorry for the abbreviation. I'm talking about the
shallow copy, still it's a copy. Unnecessary in my case and the worst part
in my scenario is the creation (allocation) and deletion of a very large
number of lists of moderate size (a few hundred objects) generated due to
slices, while I only need to have a restricted view on the original list.
The islice class partially solves the problem as I mentioned in the previous
emails.

Cheers,
Themis


On Wed, Nov 18, 2009 at 3:44 PM, Ethan Furman  wrote:

> [email protected] wrote:
> > Hi,
> >
> > I was looking for a facility similar to slices in python library that
> > would avoid the implicit creation of a new list and copy of elements
> > that is the default behaviour. Instead I'd rather have a lazy iteratable
> > object on the original sequence. Well, in the end I wrote it myself but
> > I was wondering if I missed sth in the library. If I didn't is there a
> > particular reason there isn't sth like that? I find it hard to believe
> > that all slice needs have strictly copy semantics.
> >
> > Cheers,
> > Themis
>
> Two questions:  1) What is "sth"?  and 2), What copy?
>
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> (Intel)]
> In [1]: class dummy(object):
>...: pass
>...:
>
> In [2]: a = dummy()
> In [3]: b = dummy()
> In [4]: c = dummy()
> In [5]: d = dummy()
> In [6]: e = dummy()
> In [7]: list1 = [a, b, c, d, e]
> In [8]: list1
> Out[8]:
> [<__main__.dummy object at 0x0130C510>,
>  <__main__.dummy object at 0x013F1A50>,
>  <__main__.dummy object at 0x00A854F0>,
>  <__main__.dummy object at 0x00A7EF50>,
>  <__main__.dummy object at 0x00A7E650>]
>
> In [9]: list2 = list1[1:3]
> In [10]: list2
> Out[10]:
> [<__main__.dummy object at 0x013F1A50>,
>  <__main__.dummy object at 0x00A854F0>]
>
> In [11]: list2[0] is list1[1]
> Out[11]: *True*
> In [12]: list2[1] is list1[2]
> Out[12]: *True*
>
> No copying of items going on here.  What do you get?
>
> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-19 Thread tbourden
No I'm well aware that there is no deep copy of the objects and the lists
only keep references to the objects and in essence they have the same
objects in there. But this doesn't mean they are the same list.
Modifications to slices are not written back to the original list.

x = range(5)
y = x[1:3]
y[0] = 13
x[1] == y[0]  --> False

Of course if I modify the object in the slice then the original list will
see the change, but this is not what I was saying. Second and more
importantly it's the performance penalty from allocating a large number of
lists produced from the slices and the copy of the references. islice does
not have this penalty, it should only instantiate a small object that
iterates on the original list.

Themis


On Thu, Nov 19, 2009 at 3:00 AM, Rami Chowdhury wrote:

>
> I'm not sure you're understanding the point others have been making. A
> list item is merely another reference to an existing object -- it
> doesn't copy the object in any way.
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list