【发布时间】:2015-04-29 11:40:10
【问题描述】:
我正在导入 CSV 文件并搜索每个元素以匹配正则表达式。如果为真,我想删除该元素。但是我想通过索引而不是值来删除元素,特别是因为通过索引删除更快。
with open(file, "r", newline="") as fh:
data = csv.reader(fh)
s_data = sorted(data, key=operator.itemgetter(0))
regex = re.compile("\D")
# Search for any non-numeric characters in each index and discard the element, if found.
for element in s_data:
if regex.search(element[0]) is not None:
pop(element) # <-- this is the problem
如何通过索引删除匹配的元素?
【问题讨论】: