Another Python koan: modifying a list

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Another Python koan: modifying a list

Casey Callendrello
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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Damon McCormick-2
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 <[hidden email]> 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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Brian Rue-2
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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Casey Callendrello
In reply to this post by Damon McCormick-2
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 <[hidden email]> 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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies



_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Jason Culverhouse
In reply to this post by Brian Rue-2

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

On Tue, Nov 15, 2011 at 7:52 PM, Damon McCormick <[hidden email]> 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 <[hidden email]> 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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies

_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Filip Machi

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

_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Another Python koan: modifying a list

Dirk Bergstrom-4
At Wed, 16 Nov 2011 12:13:13 -0800,Filip Machi wrote:
> 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"]
>  >>> ",".join(args).split(",")
> ['1', '2', '3', '4', '5', '6', '7']

Ding! We have a winner!

Fil, the check for your prize is in the mail...

--
       --------------------------------------
      Dirk Bergstrom           [hidden email]
             http://otisbean.com/
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies