[Tutor] packing a list of lists
vince spicer
vinces1979 at gmail.com
Fri Aug 28 17:20:30 CEST 2009
On Fri, Aug 28, 2009 at 9:18 AM, vince spicer <vinces1979 at gmail.com> wrote:
>
>
> On Fri, Aug 28, 2009 at 8:05 AM, kevin parks <kp8 at me.com> wrote:
>
>> Back to python after a long long layoff. So i am running into some
>> beginner's confusion...
>>
>> I am trying to plot a list of numbers in gnuplot.py. To do that I am
>> trying to pack the list with an index by iterating over the list so i can
>> get something like:
>>
>> foo = [12, 11, 9, 6, 2, 9, 3, 8, 12, 3, 5, 6]
>>
>> [ [1, 12], [2, 11], [3, 9], [4, 6], [5, 2], [6, 9], [7, 3], [8, 8] ... ]
>>
>> So that i have x, y pairs to plot. When i print in my func i get the right
>> thing, for each item (note scaffolding) yet when i reurn the whole list i
>> just get the last pair repeated over and over.
>>
>> I am not sure why this is.
>>
>>
>> def pack(in_seq):
>> out_list=[]
>> x = 1
>> ll=[1, 1]
>> for each in in_seq:
>> ll[0] = x
>> ll[1] = each
>> out_list.append(ll)
>> #print ll
>> x = x + 1
>> print out_list
>>
>>
>> # function declarations would go here
>> def test():
>> """test function - say what this does here and skip a line
>>
>> Keyword arguments:
>> none
>> """
>>
>> print
>> foo = minus(200)
>> plot_me = pack(foo)
>> #print foo
>> print
>> print plot_me
>>
>>
>> if __name__ == "__main__":
>> test()
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> Although I didn't test your code, I think what you are trying to accomplish
> can be done using enumerate cleaner
>
>
> def pack(foo):
> out = []
> for x,y in enumerate(foo, 1):
> out.append((x,y))
> return out
>
>
>
>
Or even cleaner with list comprehension
def pack(foo):
return [x for x in enumerate(foo, 1)]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090828/ab9d1a3a/attachment.htm>
More information about the Tutor
mailing list