Python使用多个if获取生成器表达式中的下一个项目失败

时间:2013-09-26 07:25:59

标签: python regex filter generator

问题:我有一个目录中的文件列表,我想检索第一个文件 匹配所有三个子串标准。 我根据此示例中的案例1解决方案来查找匹配条件的第一个项目 来自find first sequence item that matches a criterion

问题:但是,我发现如果我在生成器表达式中将if-checks的数量扩展为3, 然后我得到一个python:Python / compile.c:3437:stackdepth_walk:断言`深度> = 0'失败。 中止(核心倾销)

问题:这对我来说很特殊,因为我只测试一些条件,它 似乎不应该导致堆栈断言。有人可以解释 为什么会这样?

下面的案例1再现了错误

案例2显示此方法仍然适用于两个if检查

案例3显示,如果我打破列表理解和下一次调用,此方法将起作用。

案例4是相同检查的替代解决方案,但作为正则表达式,并且有效。

#! /usr/bin/python
import re

files_in_dir = ['dp2_syouo_2013-05-16_0000.csv', 
                'dp1_torishima_2013-05-21_0000.csv', 
                'dp2_torishima_2013-05-22_0000.csv', 
                'dp1_hirokawa_2013-05-21_0000.csv', 
                'dp2_hirokawa_2013-05-22_0000.csv', 
                'dp2_syouo_2013-05-17_0000.csv', 
                'dp2_syouo_2013-05-18_0000.csv']

dp_string = "dp2"
date_string = "2013-05-22"
location_string = "torishima"

# case 1: Three filter checks, stackdepth_walk: Assertion
#python: Python/compile.c:3437: stackdepth_walk: Assertion `depth >= 0' failed.
# Abort (core dumped)
file_matched_1 = next( (file_in_dir for file_in_dir 
                        in files_in_dir 
                        if dp_string in file_in_dir                                             
                        if location_string in file_in_dir
                        if date_string in file_in_dir), None)
print "case 1: " + file_matched_1;


# case 2: Two filter checks, works fine
file_matched_2 = next( (file_in_dir for file_in_dir 
                        in files_in_dir 
                        if dp_string in file_in_dir                                             
                        if location_string in file_in_dir
                        ), None)
print "case 2: " + file_matched_2

# case 3: Generate the list first with three filters, then get the first item
files_matched_3 = [file_in_dir for file_in_dir 
                        in files_in_dir 
                        if dp_string in file_in_dir                                             
                        if location_string in file_in_dir
                        if date_string in file_in_dir]
file_matched_3 = next(iter(files_matched_3))
print "case 3: " + file_matched_3

# case 4: Put the three checks into a regex
date_location_regex = r'' + dp_string + '*.' + location_string + '*.' + date_string
file_matched_4 = next( (file_in_dir for file_in_dir 
                        in files_in_dir 
                        if re.search(date_location_regex, file_in_dir)), None)
print "case 4: " + file_matched_4

1 个答案:

答案 0 :(得分:1)

您使用的if语句太多了。它从来没有发生在我身上,但是谷歌搜索我看到了一些关于它的帖子,说(你用其他2个测试证实了这一点)你需要减少你使用的if语句的数量。

坦率地说,我无法理解为什么你这样做。更好的方法是从这三个子串中组装文件名。

dp_string = "dp2"
date_string = "2013-05-22"
location_string = "torishima"
file_string = '{0}_{1}_{2}_0000.csv'.format(dp_string, location_string, date_string)

file_matched_1 = next( (file_in_dir for file_in_dir 
                        in files_in_dir 
                        if file_string in file_in_dir
                       ), None)
print "case 1: " + file_matched_1;

修改

看起来像是带有centOS的python 2.6.6的已知错误? (Link