[Baypiggies] Another Python koan: modifying a list

Casey Callendrello c1 at caseyc.net
Wed Nov 16 05:02:05 CET 2011


It's a typo.I meant mylist[i:i+1] = items. And, of course, i += 1. Long day.



On 11/15/11 7:52 PM, Damon McCormick wrote:
> Casey,
>
> What is the intent of the following line?
>
>   x[i:i+1] = items
>
> Namely, what is the purpose of blowing away item i of x every time you 
> insert a new list of items into x?
>
> Is it possible that you mean something more like the following?
>
>   x.extend(items)
>
> If so, you could just do this:
>
>   for element in mylist:
>       items = element.split(',')
>       x.extend(items)
>
> Or even this:
>
>   x = [item for element in mylist for item in element.split(,)]
>
> -Damon
>
>
> On Tue, Nov 15, 2011 at 7:35 PM, Casey Callendrello <c1 at caseyc.net 
> <mailto:c1 at caseyc.net>> wrote:
>
>     Howdy there,
>     Another python puzzle for everyone. I've been trying to come up
>     with a Pythonic (i.e. no index variable) way of solving this.
>
>     I have a list consisting of one or more independent arguments to a
>     specific command-line option. Think ./foo.py -a 1 -a 15 -a 1203.
>     Argparse handles this fine. However, a few of my users prefer to
>     specify arguments separated by commas instead. This is a fairly
>     reasonable on their part, as about half of the tools we use like
>     it one way, and half prefer the other. I don't mind supporting
>     both, and the input data can never have a comma otherwise.
>
>     So, is there another way to express this code? In reality, the
>     number of arguments is very small, so duplicating the array is no
>     big deal. I'm still interested in a cleaner solution :-)
>
>     i = 0
>     while i < len(mylist):
>        items = mylist[i].split(',')
>        if len(items) > 1:
>            x[i:i+1] = items
>        i++
>
>     --Casey Callendrello
>
>     _______________________________________________
>     Baypiggies mailing list
>     Baypiggies at python.org <mailto:Baypiggies at python.org>
>     To change your subscription options or unsubscribe:
>     http://mail.python.org/mailman/listinfo/baypiggies
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20111115/4cece401/attachment-0001.html>


More information about the Baypiggies mailing list