python return None

python return None

我正在尝试 return 来自函数的对象,采用字典格式。它似乎在函数内部工作正常,但是 return "None".

============================================= ====== 打印结果为::

{'tree1': [<ROOT.TH1D object ("root2_tree_tree1_px_hist") at 0x7fa40eec05b0>, <ROOT.TH1D object ("root2_tree_tree1_py_hist") at 0x7fa40eec0cd0>, <ROOT.TH1D object ("root2_tree_tree1_pz_hist") at 0x7fa40eec13f0>], 'tree2': [<ROOT.TH1D object ("root2_tree_tree2_px_hist") at 0x7fa40eec2620>, <ROOT.TH1D object ("root2_tree_tree2_py_hist") at 0x7fa40eec2f20>, <ROOT.TH1D object ("root2_tree_tree2_pz_hist") at 0x7fa40eeb4850>]}

{'tree1': [None, None, None], 'tree2': [None, None, None]}

所以,现在我很困惑。它在功能上打印正确的值,但 returning 不正确。 代码如下。

def set_histograms():
    .
    .
    .
    print(DichistList)  # this would correctly print dictionary 
    return DichistList

def main():
    DICHISTLIST = set_histograms()
    print(DICHISTLIST)  # this is printing "None", why?

所以,这可能与您正在使用 ROOT 的事实有关,而 ROOT 和 python 除非您使用特定的软件包,否则效果不佳。我强烈推荐查看 root_numpy,我在研究粒子物理学时广泛使用它。

示例:读取包含 ttree mytree 的 myfile.root:

from root_numpy import root2array
myarray = root2array( 'myfile.root', 'mytree' ) )

您可以立即将其转换为 pandas 数据帧:

df = pd.DataFrame( myarray )