For循环在python函数中不起作用

伊塔洛·伊格纳西奥(Italo Ignacio)

我尝试创建一个带有内部循环的函数。该脚本在没有函数的情况下起作用,但是声明python函数不起作用。(原始脚本更长,但是我认为这部分就足够了)

import numpy as np
import math as mt
from sympy import*
import fractions
init_printing(use_latex='mathjax')
PHnumbers=4
PHnumbers2=2
statetype= 1
for d in range(1,9):                                                                                                     
    modes=d+1
    if statetype==1:                                                                                                 
       comb=modes 
    elif statetype==2:
        comb=int((modes*(modes+1))/2)
    exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
    for i in range(modes):
        exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')                                                          
        exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])') 
        exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')                                           
        exec(f'v{i+1}=[[0]]*modes')                                                                                        
        exec(f'Phasesv{d}[0]*=0')
    for i in range(modes):
        exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')                     
    conteo = d
    for j in range(modes):
        for i in range(modes):
            if j<i:
                conteo = conteo + 1
                exec(f'v{conteo+1}=[[0]]*modes')
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')  
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')  
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
        exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')

对于范围(len(Vec4))中的i:print(Vec4 [i])先前的脚本有效,到目前为止没有问题。现在,我声明python函数:

def metro(PHnumbers):                                                                                                           
    statetype=1
    PHnumbers2=2
    for d in range(1,9):                                                                                                     
        modes=d+1
    if statetype==1:                                                                                                 
        comb=modes 
    elif statetype==2:
        comb=int((modes*(modes+1))/2)
    exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
    for i in range(modes):
        exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')                                                          
        exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])') 
        exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')                                           
        exec(f'v{i+1}=[[0]]*modes')                                                                                        
        exec(f'Phasesv{d}[0]*=0')
    for i in range(modes):
        exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')                     
    conteo = d
    for j in range(modes):
        for i in range(modes):
            if j<i:
                conteo = conteo + 1
                exec(f'v{conteo+1}=[[0]]*modes')
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')  
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')  
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
        exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')
for i in range(len(Vec4)):
    print(Vec4[i])

第二个代码显示问题:“ theta_1未定义”

西尔维奥·梅奥洛(Silvio Mayolo)

文档中exec

注意:默认本地语言的行为与下面对函数locals()的描述相同:不应尝试对默认本地语言字典进行修改。如果需要在函数exec()返回后查看代码对本地语言的影响,请传递一个明确的本地语言字典。

因此,从本质上讲exec,出于解释器效率的原因,您不能使用本地变量。相同的限制不适用于全局变量,这就是您的第一个版本起作用的原因。

至于该怎么做:很好,这些exec电话是非常不必要的。只需正常执行Python代码,而无需进行混乱的exec调用。老实说,它可能也会运行得更快。在所有将相似名称的变量分类在一起的地方,只需根据用例将它们替换为单个数组或字典即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在循环中调用函数在Python中不起作用

来自分类Dev

在python中循环并创建JSON文件的函数不起作用

来自分类Dev

For 循环在 Python 中的函数内部不起作用

来自分类Dev

“ for”循环在python 3.6中不起作用

来自分类Dev

For 循环在 python 3 中不起作用

来自分类Dev

函数在python中不起作用

来自分类Dev

Python 函数工作正常,但在 for 循环时不起作用

来自分类Dev

For循环在带参数的函数中不起作用

来自分类Dev

For 和 while 循环在全局定义的函数中不起作用

来自分类Dev

PHP 函数“...几天前”在“While 循环”中不起作用

来自分类Dev

Python-函数中的第二个“ for”循环不起作用

来自分类Dev

python函数不起作用

来自分类Dev

Python replace()函数不起作用-尽管在新函数中

来自分类Dev

atoi函数在for循环中不起作用

来自分类Dev

简单函数在 for 循环之外不起作用

来自分类Dev

循环中的Readline()在python中不起作用

来自分类Dev

我的While循环在python中不起作用

来自分类Dev

For循环在带有csv文件的python中不起作用

来自分类Dev

虽然循环小写程序在 python 3.6.4 中不起作用

来自分类Dev

为什么嵌套的 for 循环在 Python 中不起作用?

来自分类Dev

HyperVolume函数在Python 3中不起作用

来自分类Dev

为什么我的def函数在Python中不起作用?

来自分类Dev

简单的Python代码在Zapier Code函数中不起作用

来自分类Dev

在函数中时,Python Serial.write不起作用

来自分类Dev

为什么python中的sum函数不起作用?

来自分类Dev

Python-If语句在def()函数中不起作用

来自分类Dev

HyperVolume函数在Python 3中不起作用

来自分类Dev

python中的函数__init__不起作用

来自分类Dev

sizeof函数在函数中不起作用

Related 相关文章

热门标签

归档