【问题标题】:Perl to Python: hash to nested dictionary and cx_OraclePerl 到 Python:散列到嵌套字典和 cx_Oracle
【发布时间】:2012-12-07 19:19:08
【问题描述】:

在将数据从 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] 结果就是 {}....

【问题讨论】:

    标签: python perl dictionary cx-oracle


    【解决方案1】:

    在python字典项访问使用dictionary[key];所以你需要更正sphashtmphash 的行:

    sphash[result[7]] = 'x'
    tmphash[result[0] + '_' + result[1] + '_' + result[2] + '_' + result[3] + '_' + datetime + '_' +
            result[6]][result[7]] = tmpln
    

    这假设tmphash[somekey] 本身已经是一个字典。您可以将其定义为 defaultdict:

    from collections import defaultdict
    
    tmphash = defaultdict(dict)
    

    现在每个tmphash 键在访问时自动成为另一个dict

    【讨论】:

    • 编辑后的代码看起来如何?使用 set vs sphash 有什么好处?
    • @VictoriaPrice:由于您只使用 dict 来跟踪唯一键,因此使用集合更有效(无需存储所有 x 值)。
    【解决方案2】:

    尽我所能纠正你的 python 代码:

    # I have no idear what this is suppose to do
    # i guess you are trying to mimic the array assignment in
    # the while loop...but I think you are doing it wrong, please update your
    # poste so we can make sense of it
    desc=[d[0] for d in cursor.description]
    # makes a list of dicts with the entire desc array as key and one line as value?!?!
    result=[dict(zip(desc,line)) for line in cursor]
    
    tmphash = {}
    spset=set() # if you want an spset instead of hash
    while (True): # this should probably be a for loop with your array
        datetime="%s-%s" % results[4:6]
        tmpln='_'.join(result[8:10])
        # here you probberbly want to add to a set instead of making a hash mapping to something useless
        sphash[result[7]]='x'
        # or
        spset.add(result[7])
        # .= is a string concatination operator in perl
        # += can also concatinate strings in python
        tmphash['_'.join(result[0:4]+[datetime]+result[6:8]] += tmpln
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      相关资源
      最近更新 更多