python - Lists of tuples paired with dictionary keys, is this a good method of data classification? -


i new python, , setting large dataset work on, , wanted check , make sure doing in optimized manner possible, right pretty sure not. have python dictionary set so.

list1 = [1,2],[3,4],[4,5] list2 = [10,20],[30,40],[50,60] list3 = [100,200],[300,400],[400,500] 

these list created programmatically (and larger in reality), , sorted dictionary follows:

l_dict = {"l1":list1,"l2":list2,"l3":list3}  print l_dict       `{'l2': ([10, 20], [30, 40], [50, 60]), 'l3': ([100, 200], [300, 400], [400, 500]), 'l1': ([1, 2], [3, 4], [4, 5])}` 

i have dicitonary set same, except different numbers , object name (call k_dict). here questions: simplest way array/list of first object in each tupple? code follows:

#gets first tuple value each list  listnames = ["l1","l2","l3"] i=10 while(i < len(listdict)):  x,a in l_dict[listnames[i]]:   return a[0]  i+=1 

which working

should return (1,3,4,10,30,50,100,300,400) except spaced newlines

i have tried dict comprehension

print {x[0] x in ldict[listnames]} 

which never worked me, , have tried many similar variations wouldn't work either.

also, dictionary way set data up? couldn't find other examples of list of tuples inside dictionaries, leads me believe there might dataframe/other manner of storing data easier use. using python 2.7.1

use list comprehension:

in [16]: [y[0]  x in listnames y in l_dict[x]] out[16]: [1, 3, 4, 10, 30, 50, 100, 300, 400] 

above list comprehension equivalent to:

in [26]: lis=[]  in [27]: x in listnames:    ....:     y in l_dict[x]:    ....:         lis.append(y[0])    ....:           in [28]: lis out[28]: [1, 3, 4, 10, 30, 50, 100, 300, 400] 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -