[Baypiggies] Another Python koan: modifying a list

Filip Machi fil at macfellow.com
Wed Nov 16 21:13:13 CET 2011


On Nov 16, 2011, at 9:39 AM, Jason Culverhouse wrote:

>
> On Nov 15, 2011, at 7:59 PM, Brian Rue wrote:
>
>> 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']
>
> That's pretty clever, I never used sum that way, i.e. concatenating  
> a string by using the list's + operator.
>
> this is similar but without the "magic"
>
> from itertools import chain
> chain.from_iterable( x.split(',') for x in args)
>
> Jason

Those are both pretty clever. The following is not nearly so clever. I  
think it's simpler because it uses just basic string and list  
operations. I think it fulfills the requirement that "... the input  
data can never have a comma otherwise."

 >>> args = ["1", "2", "3,4", "5,6,7"]
 >>> args
['1', '2', '3,4', '5,6,7']
 >>> ",".join(args).split(",")
['1', '2', '3', '4', '5', '6', '7']

-Fil


>> 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
>>
>>
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> 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
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies



More information about the Baypiggies mailing list