[Tutor] is there a better way to do this?

Chris Roy-Smith chris_roysmith at internode.on.net
Mon May 9 18:53:42 EDT 2016


On 10/05/16 07:03, Ondřej Rusek wrote:
> Dne 9.5.2016 v 10:13 Chris Roy-Smith napsal(a):
>> Hi
>> Python 3.4 Linux (ubuntu)
>>
>> This code does what I want.
>> curs is the result of a mysql query
>>
>>
>> data = [[" " for x in range(9)] for y in range(count)]
>> for (ddate, mood, walk, lag, sleep) in curs:
>>         data[row][0]=ddate
>>         data[row][1]=mood
>>         data[row][2]=walk
>>         data[row][3]=lag
>>         data[row][4]=sleep
>>         row +=1
>>
> if you want 'lists in list' (like your solution):
>
> data = []
> for ddate, mood, walk, lag, sleep in curs:
>     data += [ [ddate, mood, walk, lag, sleep] ]
>
> or 'tuples in list':
>
> data = []
> for ddate, mood, walk, lag, sleep in curs:
>   data += [ (ddate, mood, walk, lag, sleep) ]
>
> but for 'tuples in list'... simple:
>
> data = []
> for record in curs:
>   data += [record]
>
>
Thanks,
I hadn't considered having a second list of lists for my calculations,
Your solution is the sort of thing I was looking for.


More information about the Tutor mailing list