[英]delete occurence in list with regex in python
So I'm reading an .avi file and storing it's index into a list. 因此,我正在读取.avi文件并将其索引存储到列表中。 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 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. Aaand它什么也不做,当我希望弹出第三个元素时,列表保持不变。
I don't think it matches anything because even when I set the regex1 to : 我认为它不匹配任何东西,因为即使将regex1设置为:
b'.*' 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. Python不适用于内置的正则表达式。 You need to import regex
module. 您需要导入regex
模块。
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.