在将数据从 Oracle 数据库读取到数组后,我需要将 perl 哈希转换为嵌套的键控 Python 字典。最有效的方法是什么?现有的 Perl 代码(我根本不懂 Perl)看起来像这样:
# read the data and place into a species hash (sphash) and a data hash (tmphash)
my (@lnarr, %sphash, %tmphash, $tmpln, $tmpsel, $x, $datetime) ;
while (@lnarr = $csr->fetchrow_array) {
$datetime = $lnarr[4].'-'.$lnarr[5] ;
$tmpln = join '_', $lnarr[8], $lnarr[9] ;
$sphash{$lnarr[7]} = 'x';
$tmphash{$lnarr[0].'_'.$lnarr[1].'_'.$lnarr[2].'_'.$lnarr[3].'_'.$datetime.'_'.$lnarr[6]}{$lnarr[7]} .= $tmpln ;
} #while line
我不确定创建嵌套字典的最有效途径是什么......任何指导都会很棒。
我的初始代码看起来像这样,但我知道它可能是非常错误的(我也只是在学习 python):前几行是根据这篇文章将 Oracle 输出中的元组读取到字典中:here
#this is creating the dictionary from the data
cursor.execute(query, cruise6_input=cruise6_input)
desc=[d[0] for d in cursor.description]
result=[dict(zip(desc,line)) for line in cursor]
station=[r['STATION'] for r in result]
time=[r['GMT_TIME']for r in result]
svspp=[r['SVSPP'] for r in result]
expcatchwt=[r['EXPCATCHWT'] for r in result]
beglat=[r['BEGLAT'] for r in result]
expcatchnum=[r['EXPCATCHNUM'] for r in result]
cruise=[r['CRUISE'] for r in result]
towdate=[r['BEGIN_GMT_TOWDATE'] for r in result]
stratum=[r['STRATUM'] for r in result]
beglon=[r['BEGLON'] for r in result]
tmpln=zip(expcatchwt,expcatchnum)
tmphash=zip(station,time,beglat,cruise,towdate,stratum,beglon)
keys=zip(tmphash,svspp)
如何获得 tmphash[svspp]=tmpln 的输出?如果我打印 tmphash[svspp] 结果只是 {}....