Re: [Tutor] s[len(s):len(s)] = [x] ??
"Jerry Hill" <[EMAIL PROTECTED]> wrote You can do it with slice assignment too: a = [1, 2, 3, 4] a[1:3] = [[7, 8]] a [1, [7, 8], 4] Now, which way is better? The answer depends on context. If, conceptually, you are removing two elements from a list, then adding a new element which is itself a list, then doing it with remove and insert is probably best. Even in that case I'd replace themultiple removes with a slice: L = [1,2,3,4] L[1:3] = [] L.insert(1,[7,8]) L [1, [7, 8], 4] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
Thanks, Ryan, for detailed explanation; I'm learning Python now, too, so I don't know exactly how stuff works. []'s Douglas On Mon, Jun 30, 2008 at 18:33, Lie Ryan <[EMAIL PROTECTED]> wrote: >> You can do it with slice assignment too: >> >>> a = [1, 2, 3, 4] >> >>> a[1:3] = [[7, 8]] >> >>> a >> [1, [7, 8], 4] >> >> Now, which way is better? The answer depends on context. The best >> way to write it is in the manner that it makes the most sense to >> someone reading your program (including you, several years later)! > > I think that the current behavior in python makes sense according to > python's slicing model: which is cursor-like behavior. List slice (and > indexing) in python is done on the model of a cursor: > > 0 1 2 3 > +---+---+---+---+ > | A | B | C | D | > +---+---+---+---+ > -4 -3 -2 -1 0 > > Cursor-like behavior of list in python is similar to the "typing > cursor", if you, for example, highlights letter B and C (i.e. taking > slice [1:3]) then pasted another string of letters, the result would be > the pasted string replaced the highlighted object (i.e. the slice is > gone and the inserted list become a sublist that replaced the sliced > one). If the pasted list have been inserted instead, it wouldn't fit the > cursor model. > > On an aside, I'm interested in seeing how this would be parsed by python > l = [1, 2, 3, 4, 5, 6, 7, 8, 9] > l[2:8:2] = [...] > > It turns out that assigning to extended slice notation require that you > to have the same length sequence on both sides, just as expected, python > doesn't do some voodoo magic for this since: "In the face of ambiguity, > refuse the temptation to guess" and "There should be one -- and > preferably only one -- obvious way to do it" [import this]. > . > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
> You can do it with slice assignment too: > >>> a = [1, 2, 3, 4] > >>> a[1:3] = [[7, 8]] > >>> a > [1, [7, 8], 4] > > Now, which way is better? The answer depends on context. The best > way to write it is in the manner that it makes the most sense to > someone reading your program (including you, several years later)! I think that the current behavior in python makes sense according to python's slicing model: which is cursor-like behavior. List slice (and indexing) in python is done on the model of a cursor: 0 1 2 3 +---+---+---+---+ | A | B | C | D | +---+---+---+---+ -4 -3 -2 -1 0 Cursor-like behavior of list in python is similar to the "typing cursor", if you, for example, highlights letter B and C (i.e. taking slice [1:3]) then pasted another string of letters, the result would be the pasted string replaced the highlighted object (i.e. the slice is gone and the inserted list become a sublist that replaced the sliced one). If the pasted list have been inserted instead, it wouldn't fit the cursor model. On an aside, I'm interested in seeing how this would be parsed by python l = [1, 2, 3, 4, 5, 6, 7, 8, 9] l[2:8:2] = [...] It turns out that assigning to extended slice notation require that you to have the same length sequence on both sides, just as expected, python doesn't do some voodoo magic for this since: "In the face of ambiguity, refuse the temptation to guess" and "There should be one -- and preferably only one -- obvious way to do it" [import this]. . ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
On Mon, Jun 30, 2008 at 4:47 PM, Dick Moores <[EMAIL PROTECTED]> wrote: > Show me a better way? You can do it with slice assignment too: >>> a = [1, 2, 3, 4] >>> a[1:3] = [[7, 8]] >>> a [1, [7, 8], 4] Now, which way is better? The answer depends on context. The best way to write it is in the manner that it makes the most sense to someone reading your program (including you, several years later)! If, conceptually, you are removing two elements from a list, then adding a new element which is itself a list, then doing it with remove and insert is probably best. On the other hand, if you are picking two elements out of the list (a slice) and replacing it with a new sublist, then the slice assignment may make more sense to the reader. -- Jerry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
At 01:39 PM 6/30/2008, Dick Moores wrote: At 11:01 PM 6/29/2008, wesley chun wrote: > > e.g. can you predict the result of the following operations without trying it? > > > > a = [1, 2, 3, 4] > > a[1:3] = [7, 8] > > print a > > [1, 7, 8, 4] Whew! > (I really wasn't positive that it shouldn't be [1, [7, 8], 4] !) good job dick! of course, you *know* i'm going to ask this... how *do* you get it to be [1, [7, 8], 4] given the original 'a'? :-) I had to look at section 5.1 in the Python tutorial, for insert() and remove(), but didn't try them out. How about a = [1,2,3,4] a.remove(2) a.remove(3) a.insert(1,[7,8]) Hey! >>> a = [1,2,3,4] >>> a.remove(2) >>> a.remove(3) >>> a.insert(1,[7,8]) >>> a [1, [7, 8], 4] >>> Show me a better way? Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
At 11:01 PM 6/29/2008, wesley chun wrote: > > e.g. can you predict the result of the following operations without trying it? > > > > a = [1, 2, 3, 4] > > a[1:3] = [7, 8] > > print a > > [1, 7, 8, 4] Whew! > (I really wasn't positive that it shouldn't be [1, [7, 8], 4] !) good job dick! of course, you *know* i'm going to ask this... how *do* you get it to be [1, [7, 8], 4] given the original 'a'? :-) I had to look at section 5.1 in the Python tutorial, for insert() and remove(), but didn't try them out. How about a = [1,2,3,4] a.remove(2) a.remove(3) a.insert(1,[7,8]) Next one? Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
> > e.g. can you predict the result of the following operations without trying > > it? > > > > a = [1, 2, 3, 4] > > a[1:3] = [7, 8] > > print a > > [1, 7, 8, 4] Whew! > (I really wasn't positive that it shouldn't be [1, [7, 8], 4] !) good job dick! of course, you *know* i'm going to ask this... how *do* you get it to be [1, [7, 8], 4] given the original 'a'? :-) cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
At 03:39 PM 6/29/2008, John Fouhy wrote: On 28/06/2008, Dick Moores <[EMAIL PROTECTED]> wrote: > I'm very familiar with appending x to a list, s, using s.append(x), however, > I've never understood what the docs mean by > > s.append(x) same as s[len(s):len(s)] = [x] In addition to Douglas's comment, do you understand how assignment with slices works? e.g. can you predict the result of the following operations without trying it? a = [1, 2, 3, 4] a[1:3] = [7, 8] print a [1, 7, 8, 4] Whew! (I really wasn't positive that it shouldn't be [1, [7, 8], 4] !) Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
At 03:39 PM 6/29/2008, John Fouhy wrote: On 28/06/2008, Dick Moores <[EMAIL PROTECTED]> wrote: > I'm very familiar with appending x to a list, s, using s.append(x), however, > I've never understood what the docs mean by > > s.append(x) same as s[len(s):len(s)] = [x] In addition to Douglas's comment, do you understand how assignment with slices works? e.g. can you predict the result of the following operations without trying it? a = [1, 2, 3, 4] a[1:3] = [7, 8] print a I can't make much more of a fool of myself than I already have on this list, so here goes: [1, 7, 8, 4] But it's tough to hit that Send button.. Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
On 28/06/2008, Dick Moores <[EMAIL PROTECTED]> wrote: > I'm very familiar with appending x to a list, s, using s.append(x), however, > I've never understood what the docs mean by > > s.append(x) same as s[len(s):len(s)] = [x] In addition to Douglas's comment, do you understand how assignment with slices works? e.g. can you predict the result of the following operations without trying it? a = [1, 2, 3, 4] a[1:3] = [7, 8] print a -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
At 06:27 PM 6/27/2008, Douglas Drumond wrote: >>> s = [1,2,3] >>> x = 5 >>> s[len(s):len(s)] = [x] # (1) >>> s [1, 2, 3, 5] When you did s[len(s):len(s)] you got the slice begining at len(s) with end at len(s) - 1, ie, nothing. At step (1), len(s) = 3, so you did s[3:3] = [x]. It meant that the slice starting at index 3 (ie, just after s' end) is (now) the list [x]. When you did it again, you got slice s[4:4], which is empty. Ah, didn't realize I was doing it again. Thanks very much! Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] s[len(s):len(s)] = [x] ??
> > >>> s = [1,2,3] > >>> x = 5 > >>> s[len(s):len(s)] = [x] # (1) > >>> s [1, 2, 3, 5] When you did s[len(s):len(s)] you got the slice begining at len(s) with end at len(s) - 1, ie, nothing. At step (1), len(s) = 3, so you did s[3:3] = [x]. It meant that the slice starting at index 3 (ie, just after s' end) is (now) the list [x]. When you did it again, you got slice s[4:4], which is empty. []'s Douglas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor