Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用多个if';s失败了_Python_Regex_Filter_Generator - Fatal编程技术网

Python使用多个if';s失败了

Python使用多个if';s失败了,python,regex,filter,generator,Python,Regex,Filter,Generator,问题:我有一个目录中的文件列表,我想检索第一个文件 匹配所有三个子字符串条件的。 我根据本例中的案例1解决方案,找到符合标准的第一项 从 问题:但是,我发现如果在生成器表达式中将if检查的数量扩展到三个, 然后我得到一个python:python/compile.c:3437:stackdepth\u walk:Assertion'depth>=0'失败。 中止(堆芯转储) 问题:这是我特有的,因为我只测试了几个条件,所以 似乎不应该导致堆栈断言。有人能解释一下吗 为什么会发生这种情况 下面的案

问题:我有一个目录中的文件列表,我想检索第一个文件 匹配所有三个子字符串条件的。 我根据本例中的案例1解决方案,找到符合标准的第一项 从

问题:但是,我发现如果在生成器表达式中将if检查的数量扩展到三个, 然后我得到一个python:python/compile.c:3437:stackdepth\u walk:Assertion'depth>=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

您使用了太多的
if
语句。这从未发生在我身上,但我在谷歌上看到了一些关于它的帖子,说(你用另外两个测试证实了这一点)你需要减少你使用的
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的已知错误?()

您的Python版本和操作系统是什么?我刚刚在Win 7上用Py 2.7运行了你的代码片段,效果很好。@Abhijit我正在运行python 2.6.6,下面的答案指出了这个版本上的已知错误。@OfirIsarael我同意更严格的匹配会更容易,但我想尝试灵活的匹配,以防周围的字符串内容和顺序发生变化。感谢您指出已知的错误。