如果Python中的语句问题

问题描述:

我试图使用多个if语句来检查某个条件,然后在Python中使用matplotlib来绘制一些数据。首先,我在目录上执行os.walk以获取文件列表,然后加载它们以最终绘制并保存图形。这里是我的代码:如果Python中的语句问题

def velocity(): 
    plt.xlabel('$\mathrm{Iterations\/or\/Time}$') 
    plt.title(r'$\mathrm{Residual\/history}$') 
    plt.grid(True) 
    plt.yscale('log') 

    if 'Ux_0' in lf: 
     print "Entered" 
     plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
    elif 'Uy_0' in lf: 
     print "Entered" 
     plt.plot(time_y, value_y, color = 'b', label = 'y-vel') 
    elif 'Uz_0' in lf: 
     print "Entered" 
     plt.plot(time_z, value_z, color = 'g', label = 'z-vel') 

     plt.legend() 
     plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
     plt.close() 

    return (time_x, value_x, lf) 
    return (time_y, value_y, lf) 
    return (time_z, value_z, lf) 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     if 'Ux_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_x = np.loadtxt(logFile) 
      time_x, value_x = data_x[:,0], data_x[:,1] 
      (time_x, value_x, lf) = velocity() 
     if 'Uy_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_y = np.loadtxt(logFile) 
      time_y, value_y = data_y[:,0], data_y[:,1] 
      (time_y, value_y, lf) = velocity() 
     if 'Uz_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_z = np.loadtxt(logFile) 
      time_z, value_z = data_z[:,0], data_z[:,1] 
      (time_z, value_z, lf) = velocity() 

logDir只有三个文件,开始和他们Ux_0Uy_0Uz_0。有趣的是,在os.walk当我printlf,我得到文件的订单Ux_0Uz_0Uy_0。现在,函数velocity()生成的数字仅具有来自Ux_0Uz_0的数据,而不具有来自Uy_0的数据。然而,在我的函数中,如果Uy_0Uz_0的顺序被颠倒,使得我有Uz_0,紧接在Ux_0后面,如下所示,我得到所有三个图。

if 'Ux_0' in lf: 
    print "Entered" 
    plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
elif 'Uz_0' in lf: 
    print "Entered" 
    plt.plot(time_z, value_z, color = 'b', label = 'z-vel') 
elif 'Uy_0' in lf: 
    print "Entered" 
    plt.plot(time_y, value_y, color = 'g', label = 'y-vel') 

    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

return (time_x, value_x, lf) 
return (time_y, value_y, lf) 
return (time_z, value_z, lf) 

我不确定是什么原因造成的。

+1

为什么'velocity'返回变量,它从全球范围发生,并以任何方式不会改变? – zehnpaard 2015-02-05 23:23:57

虽然Padraic对您的return陈述完全正确无误,但问题的实际原因是您的缩进和放置plt.savefig命令。

如果你看一下,你有你的plt.savefig声明,它只有当你到达最后elif,即当它发现在lfUz_0执行。当Uz_0是列表中的第二项时,该绘图仅在该点保存,因此最后一个数据集被绘制但未保存。

您应该有一个save_velocity()函数,您可以在最后运行。

def velocity(): 
    #Rows omitted for succinctness' sake 

    if 'Ux_0' in lf: 
     print "Entered" 
     plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
    elif 'Uy_0' in lf: 
     print "Entered" 
     plt.plot(time_y, value_y, color = 'b', label = 'y-vel') 
    elif 'Uz_0' in lf: 
     print "Entered" 
     plt.plot(time_z, value_z, color = 'g', label = 'z-vel') 

    return (time_x, value_x, lf) # Do Padraic's fixes here! 
    return (time_y, value_y, lf) 
    return (time_z, value_z, lf) 

def save_velocity(): 
    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     #Rows omitted for succinctness' sake 
     if 'Uz_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_z = np.loadtxt(logFile) 
      time_z, value_z = data_z[:,0], data_z[:,1] 
      (time_z, value_z, lf) = velocity() 

save_velocity() 

的代码位的清理:

def velocity(time, value, lf): 
    plt.xlabel('$\mathrm{Iterations\/or\/Time}$') 
    plt.title(r'$\mathrm{Residual\/history}$') 
    plt.grid(True) 
    plt.yscale('log') 

    if 'Ux_0' in lf: 
     velocity_color = 'r' 
     velocity_label = 'x-vel' 
    elif 'Uy_0' in lf: 
     velocity_color = 'b' 
     velocity_label = 'y-vel' 
    elif 'Uz_0' in lf: 
     velocity_color = 'g' 
     velocity_label = 'z-vel' 

    print "Entered" 
    plt.plot(time, value, color = velocity_color, label = velocity_label) 

    return time, value, lf 

def save_velocity(): 
    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     if any((filename in lf) for filename in ('Ux_0', 'Uy_0', 'Uz_0')): 
      logFile = os.path.join(path, lf) 
      data = np.loadtxt(logFile) 
      time, value = data[:,0], data[:,1] 
      (time, value, lf) = velocity(time, value, lf) # Not sure why you return any value at all here, do you use these values later in some way? 

save_velocity() 
+0

谢谢@zehnpaard。这个逻辑完全按照我的需要工作。 – hypersonics 2015-02-06 00:29:15

+0

没问题@Deepak,很乐意帮忙。 – zehnpaard 2015-02-06 00:38:45

你永远只能返回相同的值,所以如果你在设定值,其余都是你不可达如果基于想着你是从你的速度函数得到不同的值,你不会:

return (time_x, value_x, lf) 
    return (time_y, value_y, lf) # < unreachable 
    return (time_z, value_z, lf) # < unreachable 

一个函数在它返回一个返回语句时结束,因此只要你到达它的第一个语句就结束。

您可以返回多个元素:

return (time_x,time_y, time_z value_x, value_y, value_z, lf) 

然后用切片分配res = velocity(); a,b,c = res[2],res[3] ,res[4]等提取和任何你想要的分组。

+0

没错。应该使用'return'作为最终输出。如果你试图获得所有的值,你应该使用'print',然后返回一个值,以得到速度函数的点。 – Zizouz212 2015-02-05 23:30:38

+0

谢谢@Padraic。你能详细说明你的解释吗? – hypersonics 2015-02-05 23:57:28

+0

@Deepak,你只有'(time_x,value_x,lf)',你不能有多个返回语句。您可以返回所有元素的元组,并为返回的元组索引来设置所需的值。 – 2015-02-06 00:01:51