【问题标题】:Python: Dictionary being returned by func as string? What the heck am I doing wrong?Python:func作为字符串返回的字典?我到底做错了什么?
【发布时间】:2013-10-09 21:25:18
【问题描述】:

我在一个函数中生成一个字典,然后返回这个字典。尽管格式正确,但我似乎无法将返回的 dict 作为字典访问。它仅将数据视为字符串,即我可以打印它但不能打印 d.keys() 或 d。 items() 我到底做错了什么??????

打印为 str() 时的数据

{1:'214902885,2:2:'214902910,214902934',3:'214902935,214902959',4:'214902960,214902984',5:'214902985,214903009',6:'214903010,214903034 ', 7: '214903035,214903059', 8: '214903060,214903084', 9: '214903085,214903109', 10: '214903110,214903139'}

我尝试打印 d.items() 或 d.keys() 时出错

print bin_mapping.keys()

AttributeError: 'str' 对象没有属性 'keys'

从函数返回字典后,是否必须将其重新定义为字典?我真的很感激一些帮助,因为我非常沮丧:/

谢谢,

这里建议的是代码。我调用的函数是首先返回字典。

def models2bins_utr(id,type,start,end,strand):
  ''' chops up utr's into bins for mC analysis'''
  # first deal with 5' UTR
  feature_len = (int(end) - int(start))+1
  bin_len = int(feature_len) /10
  if int(feature_len) < 10:
   return 'null'
   #continue
  else:
  # now calculate the coordinates for each of the 10 bins
   bin_start = start
   d_utr_5 = {}
   d_utr_3 = {}
   for i in range(1,11):
    # set 1-9 first, then round up bin# 10 )
    if i != 10:
     bin_end = (int(bin_start) +int(bin_len)) -1
     if str(type) == 'utr_5':
      d_utr_5[i] = str(bin_start)+','+str(bin_end)
     elif str(type) == 'utr_3':
      d_utr_3[i] = str(bin_start)+','+str(bin_end)
     else:
      pass
     #now set new bin_start
     bin_start = int(bin_end) + 1
    # now round up last bin
    else:
     bin_end = end
     if str(type) == 'utr_5':
      d_utr_5[i] = str(bin_start)+','+str(bin_end)
     elif str(type) == 'utr_3':
      d_utr_3[i] = str(bin_start)+','+str(bin_end)
     else:
      pass
  if str(type) == 'utr_5':
   return d_utr_5
  elif  str(type) == 'utr_3':
   return d_utr_3

调用函数并尝试访问字典

def main():
  # get a list of all the mrnas in the db
  mrna_list = get_mrna()
  for mrna_id in mrna_list:
   print '-----'
   print mrna_id
   mrna_features = features(mrna_id)
   # if feature utr, send to models2bins_utr and return dict
   for feature in mrna_features:
    id = feature[0]
    type = feature[1]
    start = feature[2]
    end = feature[3]
    assembly = feature[4]
    strand = feature[5]
   if str(type) == 'utr_5' or str(type) == 'utr_3':
    bin_mapping = models2bins_utr(id,type,start,end,strand)
    print bin_mapping
    print bin_mapping.keys()

【问题讨论】:

  • 也许您应该发布返回该数据的函数?以及调用它的代码?
  • 我本来打算写的,但是代码很多...可能是截断版本..我会继续写的。
  • 检查“null”是否从未返回...
  • Ick,1 个空格缩进?您如何阅读这些东西,更不用说维护它了?
  • 谢谢乔治!!我忽略了一个非常简单的错误。非常感谢您的帮助!

标签: python string function dictionary return


【解决方案1】:

我想知道return 'null' 应该实现什么。我的猜测是,你偶尔会用错误的参数调用函数并取回这个字符串。

我建议改为抛出异常(raise Exception('Not enough arguments') 或类似的)或返回空字典。

您还应该了解repr(),因为它为您提供了有关对象的更多信息,从而使调试变得更加容易。

【讨论】:

  • “您还应该了解 repr(),因为它为您提供了有关对象的更多信息,使调试变得更加容易”谢谢,我会检查一下。
【解决方案2】:

你很早就返回了一个字符串:

bin_len = int(feature_len) /10
if int(feature_len) < 10:
    return 'null'

也许您想在这里引发异常,或者至少返回一个空字典或使用None作为标志值。

如果您使用None 进行测试:

bin_mapping = models2bins_utr(id,type,start,end,strand)
if bin_mapping is not None:
     # you got a dictionary.

【讨论】:

  • @BartoszKP:但是为什么期望能够调用.keys()呢?
  • 是的,您的建议是更好的解决方案。我只是在解释 OP 的尝试。初学者倾向于局部思考,所以可能在写这行时 OP 并没有关注如何使用这个结果:)
  • 谢谢大家.. 是的,这就是问题所在。我仍在思考如何将我的代码分块进行风格化......这是一个愚蠢的错误:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 2017-04-15
相关资源
最近更新 更多