Re-order list in Python to ensure it starts with check values.

Posted by S_Swede on Stack Overflow See other posts from Stack Overflow or by S_Swede
Published on 2010-03-05T13:39:32Z Indexed on 2010/03/08 6:21 UTC
Read the original article Hit count: 246

Filed under:
|

Dear all,

I'm reading in serial data using Pyserial, to populate a list of 17 values (1byte each) at a sampling rate of 256Hz.

The bytes I ultimately want to use are the 5th to 8th in the list. Providing no bytes are dropped, the first two values of the stream are always the same ('165','90'). I'm getting quite a few dropped values though, and my list values are shifting, so when I read the 5th-8th bytes, they aren't the correct values.

I've partially combatted this by ensuring that before the wanted segement is captured, the first few values are checked against what they should be (i.e. if mylist[0]==165 &....).
This is crude but ok since the chances of these two values appearing adjacent to each other in the list elsewhere is small. The problem is that this means as soon as the bytes shift, I'm losing a load of values, until it eventually realigns.

My question is: what code can I use to either:

a) Force the list to realign once it has been detected that it no longer starts with 165,90. (elif....).

b) Detect where '165' & '90' are (next to each other) in the list and extract the values I want in relation to their position (next but one, onwards).

Thanks in advance

S_S

Just noticed from the related Qs that I could use

mylist.append(mylist.pop(0)) 

multiple times until they are in the right place. Is there a better way that anyone can suggest?

© Stack Overflow or respective owner

Related posts about python

Related posts about list