简体   繁体   中英

Python - Match list

hey i have been searching and testing different options all day and can´t get my code to work as a want it to... tried comprehension and nestedfor loop but i can´t get it right...

zipbytehex = [(1, 0, '0x1636'), (2, 1, '0x62'), (3, 2, '0x02'), (4, 3, '0x2F'), (5, 0, '0x1637'), (6, 1, '0x19'), (7, 2, '0x02'), (8, 3, '0x2F'), (13, 0, '0x1637'), (14, 1, '0x19'), (15, 2, '0x04'), (16, 3, '0x0A'), (17, 4, '0x1B'), (18, 5, '0x47'), (19, 6, '0xFF'), (20, 0, '0x1637'), (21, 1, '0x22'), (22, 2, '0x04'), (23, 3, '0x06'), (24, 4, '0x07'), (25, 5, '0x68'), (26, 6, '0xFF'), (718, 1, '0x59'), (719, 2, '0x02'), (720, 3, '0xFF'), (721, 0, '0x163C'), (722, 1, '0x59'), (723, 2, '0x02'), (724, 3, '0xFF'), (725, 0, '0x1635'), (726, 1, '0x59'), (727, 2, '0x02'), (728, 3, '0xFF'), (729, 4, '0x0C'), (730, 5, '0x42'), (731, 6, '0x00'), (732, 7, '0xAF')]

     new_zipbytehex =[]
        for i in zipbytehex:
            if i[1] != 0:
                new_zipbytehex.append(i)
        bytehexservi = []
        byteservi = []
        for i in zipbyteservi:
            if i[1:] == (1, '0x19'):
                byteservi.append(i[:2])
        service2, adatabyte2 = zip(*byteservi)   # inverse zip
        for new_bytehex in new_zipbytehex:
            if new_bytehex[1:] == (1, '0x19'):    # 0x19, 0x22, 0x59 or 0x62
                bytehexservi.append(list(zip(new_bytehex, service2, adatabyte2)))
        for list_bytehexservi in bytehexservi:
            #print(list_bytehexservi)
            sql = """INSERT INTO tblMsgsBytes2Parameters ([p_MsgBytes],[p_Parameter],[A_Databyte]) VALUES (?,?,?)"""
            cursor.execute(sql, *list_bytehexservi)
        cursor.commit()
        cursor.close()
        con.close()

Im new to python and this is as far i got in my code and now im stuck... i´m trying to extract every tuple that has 0x19 and the once after from my list zipbytehex and then write it to Access. I have managed so that every tuple with (x, 1, '0x19') get written over to Access but not the once after (ex (5, 0, '0x1637'),(6, 1, '0x19'), (7, 2, '0x02'), (8, 3, '0x2F') from zipbytehex ) the only value i get over to Access is (6, 1, '0x19') not (7, 2, '0x02') and (8, 3, '0x2F') as i want to... i need to somehow loop through and append (7, 2, '0x02') and (8, 3, '0x2F') at the same time, but i don´t know how... can someone help me? Thanks

ex: my output:

(6, 1, '0x19')

wanted output:

(6, 1, '0x19')
(7, 2, '0x02') 
(8, 3, '0x2F')

Trying to understand your description, it seems to me that the data is divided in blocks of tuples for which the second number is increasing. This leads to the following code

def gen_blocks(seq):
    block = []
    threshold = -1
    for item in seq:
        if item[1] < threshold:
            yield block
            block = []
        threshold = item[1]
        block.append(item)
    else:
        if block: yield block

def gen_wanted(seq):
    for b in gen_blocks(seq):
        for i, item in enumerate(b):
            if item[2] == '0x19':
                yield b[i:]
                break

zipbytehex = [(1, 0, '0x1636'), (2, 1, '0x62'), (3, 2, '0x02'), (4, 3, '0x2F'), (5, 0, '0x1637'), (6, 1, '0x19'), (7, 2, '0x02'), (8, 3, '0x2F'), (13, 0, '0x1637'), (14, 1, '0x19'), (15, 2, '0x04'), (16, 3, '0x0A'), (17, 4, '0x1B'), (18, 5, '0x47'), (19, 6, '0xFF'), (20, 0, '0x1637'), (21, 1, '0x22'), (22, 2, '0x04'), (23, 3, '0x06'), (24, 4, '0x07'), (25, 5, '0x68'), (26, 6, '0xFF'), (718, 1, '0x59'), (719, 2, '0x02'), (720, 3, '0xFF'), (721, 0, '0x163C'), (722, 1, '0x59'), (723, 2, '0x02'), (724, 3, '0xFF'), (725, 0, '0x1635'), (726, 1, '0x59'), (727, 2, '0x02'), (728, 3, '0xFF'), (729, 4, '0x0C'), (730, 5, '0x42'), (731, 6, '0x00'), (732, 7, '0xAF')]

for x in gen_wanted(zipbytehex):
    print(x)

The ouptut is

[(6, 1, '0x19'), (7, 2, '0x02'), (8, 3, '0x2F')]
[(14, 1, '0x19'), (15, 2, '0x04'), (16, 3, '0x0A'), (17, 4, '0x1B'), (18, 5, '0x47'), (19, 6, '0xFF')]

as expected.

You could just build your list manually; this often gets the job done better than other tricks such as comprehension.

Bascially, don't put anything in your list unless a 0x19 has already been met.

data = []
for t in zipbytehex:
    if data:
        data.append(t)
    elif t[2] == '0x19':
        data.append(t)

That solution relies on a trick with the boolean value of a list. If the data list is empty, put the element into it iff it has a 0x19 . Else, data is not empty, which means that a 0x19 has already been put into it, so append the element, no matter what.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM