filtering lists of lists python, how to create final list? -
i have code produces data struicture looks this:
{'attributeid': '4192', 'attributelist': '', 'classid': '1014 (ap)', 'instanceid': '0', 'messagetype': '81 (getattributesresponse)', 'objectinstance': '', 'protocol': 'bsmis rx', 'rdn': '', 'transactionid': '66', 'sequences': [[], [1,'2013-02-26t15:01:11z'], [], [10564,13,388,0,-321,83,'272','05',67,67,708,896,31,128,-12,-109,0,-20,-111,-1,-1,0], [10564,13,108,0,-11,83,'272','05',67,67,708,1796,31,128,-12,-109,0,-20,-111,-1,-1,0], [10589,16,388,0,-15,79,'272','05',67,67,708,8680,31,125,-16,-110,0,-20,-111,-1,-1,0], [10589,15,108,0,-16,81,'272','05',67,67,708,8105,31,126,-14,-109,0,-20,-111,-1,-1,0], [10637,40,233,0,-11,89,'272','03',30052,1,5,54013,33,103,-6,-76,1,-20,-111,-1,-1,0], [10662,46,234,0,-15,85,'272','03',30052,1,5,54016,33,97,-10,-74,1,-20,-111,-1,-1,0], [10712,51,12,0,-24,91,'272','01',4013,254,200,2973,3,62,-4,-63,0,-20,-111,-1,-1,0], [10737,15,224,0,-16,82,'272','01',3020,21,21,40770,33,128,-13,-108,0,-20,-111,-1,-1,0], [10762,14,450,0,-7,78,'272','01',3020,21,21,53215,29,125,-17,-113,0,-20,-111,-1,-1,0], [10762,15,224,0,-7,85,'272','01',3020,21,21,50770,33,128,-10,-105,0,-20,-111,-1,-1,0], [10762,14,124,0,-7,78,'272','01',3020,10,10,56880,32,128,-17,-113,0,-20,-111,-1,-1,0], [10812,11,135,0,-14,81,'272','02',36002,1,11,43159,31,130,-14,-113,1,-20,-111,-1,-1,0], [10837,42,23,0,-9,89,'272','02',36002,1,11,53529,31,99,-6,-74,1,-20,-111,-1,-1,0,54], [13,'2013-02-26t15:02:09z'], [], [2,12,7,0,9,70,'272','02',20003,0,0,15535,0,0,0,0,1,100,100,-1,-1,0], [5,15,44,0,-205,77,'272','02',20003,0,0,15632,0,0,0,0,1,100,100,-1,-1,0], [7,25,9,0,0,84,'272','02',20002,0,0,50883,0,0,0,0,1,100,100,-1,-1,0]] }
i filtered down make list of relevant values, wanted first 2 elements of sequences if length >=22. did follows:
len22seqs = filter(lambda s: len(s)>=22, data['sequences']) uarfcnrssi = [] in range(len(len22seqs)): uarfcnrssi.append([len22seqs[i][0], len22seqs[i][1]])
an example of filtered list is:
[[10564, 15], [10564, 13], [10589, 18], [10637, 39], [10662, 38], [10712, 50], [10737, 15], [10762, 14], [10787, 9], [10812, 12], [10837, 45], [3, 17], [7, 21], [46, 26], [48, 12], [49, 24], [64, 14], [66, 17], [976, 27], [981, 22], [982, 22], [983, 17], [985, 13], [517, 9], [521, 15], [525, 11], [526, 13], [528, 14], [698, 14], [788, 24], [792, 19]]
however note need third element in each of these sub-lists. this:
[1,'2013-02-26t15:01:11z'],
i need first element of every list length of 2 appended filtered list third element, elements follow. when there new list length 2 need new value appended subsequent entries.
so final list example like, note change 13 third element upon finding list length 2:
[[10564, 15, 1], [10564, 13, 1], [10589, 18, 1], [10637, 39, 1], [10662, 38, 1], [10837, 45, 1], [3, 17, 13], [7, 21, 13], [46, 26, 13], etc]
how do this? have filter twice len >=22 , len = 2, , separate filter len >=22 wouldn't want append element 0 or 1 final list lists length 2.
i try make readable:
uarfcnrssi = [] x = none # future "third element"; please choose better name item in data["sequences"]: if len(item) == 2: x = item[0] elif len(item) >= 22: uarfcnrssi.append([item[0], item[1], x])
Comments
Post a Comment