简体   繁体   中英

delete occurence in list with regex in python

So I'm reading an .avi file and storing it's index into a list. Each element corresponds to an index movie frame. I'm working on a script to delete all the occurrences in the list that start with :

00dc\\x10\\x00\\x00\\x00

Here's a short version of the code

   list = ['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00',
        '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00',
        '00dc\x10\x00\x00\x00&\x84,\x00\x95D\x01\x00',
        '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00',
        '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

    regex1 = b'00dc\x10\x00\x00\x00.{8}'
    newlist = [x for x in list if x != regex1]

Aaand it doesn't do anything, the list stays the same when I expected the third element to be popped out.

I don't think it matches anything because even when I set the regex1 to :

b'.*'

The list stays the same. Having trouble figuring out where the issue is coming from. Thanks

Python doesn't work with regex built-in. You need to import regex module.

import re
list = ['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00',
        '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00',
        '00dc\x10\x00\x00\x00&\x84,\x00\x95D\x01\x00',
        '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00',
        '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

pattern = re.compile(b'00dc\x10\x00\x00\x00.{8}')
newlist = [x for x in list if not re.match(pattern,x)]

Output:

['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00', '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00', '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00', '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

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