[Tutor] extracting lists from lists of lists

nik my.mailing.lists at noos.fr
Tue Sep 14 15:45:42 CEST 2004


Kent Johnson wrote:

> At 12:52 PM 9/14/2004 +0200, nik wrote:
>
>> hi,
>>
>> I have a class which is just a holder for a data structure, and for 
>> some reason I've decided to hold the data in the following form;
>>
>> class myData:
>>    data = [  ["name", ""], ["age", ""], ["gender", ""] ]
>>
>> I'm not totally sure it's the best way, but it strikes me as 
>> something that can be easily manipulated into maps etc. I had started 
>> out with
>> class myData:
>>    name = ""
>>    age = ""
>>    gender = ""
>>
>> but I found that I had to put most of those items into lists to do 
>> anything with them.
>
>
> What kinds of things are you doing that you need these values in a 
> list? It sounds like maybe you are trying to pull the data out of the 
> class to pass to another function, but maybe it would be better to 
> pass the class itself around? Or maybe you should get rid of the class 
> entirely and just use a dictionary.
>

I'm thinking in terms of a C struct -  it's a handy parcel to move a set 
of data around, and quite likely there'll be stacks of sets. There's no 
extra functionality required from the set, and I'm not planning on 
deriving any other classes from this one. The dictionary seems a very 
good idea, but I think I still need to put it into a class since I'll 
have multiple instances of it - is that right? or can I do the 
equivalent of a typedef?



>> So apart from any advice on holding data like that, I was wondering 
>> if there's any cool way to get the values into a tuple?
>>
>> ie [  ["name", "john"], ["age", "88"], ["gender", "M"] ]   -> 
>> ("john", "88", "M")
>
>
> List comprehension to the rescue!
> >>> tuple( [ item[1] for item in [  ["name", "john"], ["age", "88"], 
> ["gender", "M"] ] ] )
> ('john', '88', 'M')
>

shortly after sending my first email I managed to come up with
tuple ( y for x,y in [  ["name", "john"], ["age", "88"], ["gender", "M"] 
] ] )
which is the same thing I guess. I felt very smug with myself :-)   I 
wish I could have similar smug moments with the firebird database I'm 
trying to connect this all to....

If I use a dictionary, ie what Bill said;

(stuff['name'], stuff['age'], stuff['gender']) # stuff being the dictionary object

it's going to be longer, but I realise that there'll be no confusion over the order of the resulting tuple, whereas if my user alters the order of my list it would create a messed up tuple.

nik

>> I can use a for loop probably, but I've seen people do some very 
>> clever stuff with slicing and multiple assignment (which I'm still 
>> getting to grips with, but loving). Any suggestions?
>>
>> Suggestions for holding the data differently initially are welcome 
>> too (I'd like the data to be obvious to users that they shouldn't 
>> change the names, but be able to easily add/change/manipulate the 
>> values).
>
>
> A class is good for that.
>
> Kent
>
>
>> thanks,
>> nik
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>




More information about the Tutor mailing list