2

I have code that produces a data struicture that looks like 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 then filtered this down to make a list of relevant values, I only wanted the first 2 elements of Sequences if the length was >=22. I did this as follows:

 len22seqs = filter(lambda s: len(s)>=22, data['Sequences'])
 UARFCNRSSI = []
 for i in range(len(len22seqs)):
     UARFCNRSSI.append([len22seqs[i][0], len22seqs[i][1]]) 

An example of the 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 I now note that I need a third element in each of these sub-lists. That is this:

[1,'2013-02-26T15:01:11Z'],

I need the first element of every list with length of 2 to be appended to this filtered list as a third element, for the elements that follow. But when there is a new list with length 2 then I need that new value to be appended to the subsequent entries.

So my final list example could look like, note the change to 13 for the third element upon finding another list with 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 I do this? Do i have to filter twice with len >=22 and len = 2, and a separate filter for just len >=22 as I wouldn't want to append element 0 or 1 to my final list for the lists with length 2.

2 Answers 2

4

I would try to make it readable:

UARFCNRSSI = []
x = None        # future "third element"; please choose a better name
for item in data["Sequences"]:
   if len(item) == 2:
       x = item[0]
   elif len(item) >= 22:
       UARFCNRSSI.append([item[0], item[1], x])
Sign up to request clarification or add additional context in comments.

4 Comments

i think.. .defining x before the for loop is not required?
@namit: Just a precaution. Are you sure that there will always be a two-element list before the first 22-element list?
Thanks, much better way than I had planned, much cleaner and simpler, easier to read. There will always be a 2 element list first yes, the data is the output of a command in a cli, and the 2 element list has to be there before the other list is generated. However you are very right, in the case that it is not there, due to output I have not foreseen it is best to prepare!
Oh, one simple correction, append takes one argument. UARFCNRSSI.append([item[0], item[1], x])
3

I'd go with a generator to filter your data:

def filterdata(sequences):
    add = []
    for item in sequences:        
        if len(item) == 2:
            add = [item[0]]
        elif len(item) >= 22:
            yield [item[0], item[1]] + add

You can access it like data = list(filterdata(data['Sequences']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.