我正在使用 CERN 的 pyROOT 模块做一些工作,我正在尝试将字符串数组存储为二叉树中的叶子。为此,我必须向它传递一个数组,显然,使用的不是列表或字典,而是数组模块。该模块支持标准 C 数组、字符、整数等,但是有谁知道我可以嵌套它们以获得字符串数组,或者实际上是字符数组的方法吗?还是我走得太远了,我需要暂时离开键盘 :)?
代码:
import ROOT
rowtree = ROOT.TTree("rowstor", "rowtree")
ROOT.gROOT.ProcessLine(
"struct runLine {\
Char_t test[20];\
Char_t test2[20];\
};" );
from ROOT import runLine
newline = runLine()
rowtree.Branch("test1", newline, "test/C:test2")
newline.test = ["AbcDefgHijkLmnOp","aaaaaaaaaaaaaaaaaaa"]
rowtree.Fill()
错误:
python branchtest
Traceback (most recent call last):
File "branchtest", line 14, in <module>
newline.test = ["AbcDefgHijkLmnOp","aaaaaaaaaaaaaaaaaaa"]
TypeError: expected string or Unicode object, list found
我想知道是否可以将此示例中显示的列表转换为字符串数组。
最佳答案
char 数组和 Python 字符串列表是两种截然不同的东西。
如果您想要一个包含字符数组(一个字符串)的分支,那么我建议使用 Python 的内置 bytearray
输入:
import ROOT
# create an array of bytes (chars) and reserve the last byte for null
# termination (last byte remains zero)
char_array = bytearray(21)
# all bytes of char_array are zeroed by default here (all b'\x00')
# create the tree
tree = ROOT.TTree('tree', 'tree')
# add a branch for char_array
tree.Branch('char_array', char_array, 'char_array[21]/C')
# set the first 20 bytes to characters of a string of length 20
char_array[:21] = 'a' * 20
# important to keep the last byte zeroed for null termination!
tree.Fill()
tree.Scan('', '', 'colsize=21')
tree.Scan('', '', 'colsize=21')
的输出是:
************************************
* Row * char_array *
************************************
* 0 * aaaaaaaaaaaaaaaaaaaa *
************************************
所以我们知道树正在正确地接受字节。
如果你想存储一个字符串列表,那么我建议使用 std::vector<std::string>
:
import ROOT
strings = ROOT.vector('string')()
tree = ROOT.TTree('tree', 'tree')
tree.Branch('strings', strings)
strings.push_back('Hello')
strings.push_back('world!')
tree.Fill()
tree.Scan()
tree.Scan()
的输出是:
***********************************
* Row * Instance * strings *
***********************************
* 0 * 0 * Hello *
* 0 * 1 * world! *
***********************************
在一个循环中你会想要 strings.clear()
在下一个条目中填充新的字符串列表之前。
现在,rootpy包(另见存储库 on github )提供了一种在 Python 中创建树的更好方法。这是一个示例,说明如何通过 rootpy 以“更友好”的方式使用 char 数组:
from rootpy import stl
from rootpy.io import TemporaryFile
from rootpy.tree import Tree, TreeModel, CharArrayCol
class Model(TreeModel):
# define the branches you want here
# with branchname = branchvalue
char_array = CharArrayCol(21)
# the dictionary is compiled and cached for later
# if not already available
strings = stl.vector('string')
# create the tree inside a temporary file
with TemporaryFile():
# all branches are created automatically according to your model above
tree = Tree('tree', model=Model)
tree.char_array = 'a' * 20
# attemping to set char_array with a string of length 21 or longer will
# result in a ValueError being raised.
tree.strings.push_back('Hello')
tree.strings.push_back('world!')
tree.Fill()
tree.Scan('', '', 'colsize=21')
tree.Scan('', '', 'colsize=21')
的输出是:
***********************************************************************
* Row * Instance * char_array * strings *
***********************************************************************
* 0 * 0 * aaaaaaaaaaaaaaaaaaaa * Hello *
* 0 * 1 * aaaaaaaaaaaaaaaaaaaa * world! *
***********************************************************************
查看另一个使用 TreeModel
的示例在这里使用 rootpy:
https://github.com/rootpy/rootpy/blob/master/examples/tree/model_simple.py
关于ROOT TTree 中的 Python 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691593/