[Baypiggies] Another Python koan: modifying a list

Brian Rue brian at lolapps.com
Wed Nov 16 04:59:57 CET 2011


A variation on the same theme:

>>> args = ["1", "2", "3,4", "5,6,7"]
>>> args
['1', '2', '3,4', '5,6,7']
>>> sum((x.split(',') for x in args), [])
['1', '2', '3', '4', '5', '6', '7']

On Tue, Nov 15, 2011 at 7:52 PM, Damon McCormick <damonmc at gmail.com> 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> 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
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/**mailman/listinfo/baypiggies<http://mail.python.org/mailman/listinfo/baypiggies>
>>
>
>
> _______________________________________________
> Baypiggies mailing list
> 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/f8316bc7/attachment.html>


More information about the Baypiggies mailing list