【问题标题】:What is the difference between these two lists?这两个列表有什么区别?
【发布时间】:2016-08-22 18:16:08
【问题描述】:

我有一个字典列表,在这些字典中,有时会出现一个特定的键。这个特定的键可能有一个字典作为它的值,并且在那个字典中是一个感兴趣的键值对。或者,特定键可能包含一个字典列表,其中包含感兴趣的键值对。 在尝试获取列表中感兴趣的值的过程中,我遇到了一个更基本的问题:当我尝试制作如上所述的列表时,出现类型错误。

所以我从真实数据中删减了一个列表以尽可能少,并且该列表按预期创建。也许我只是醒得太久了,但我一辈子都看不出创建的列表和没有创建的列表之间的区别。

bad_list = list[{'info1':'infoA', 'info2':'infoB'},
{'info1':'infoC', 'info2':'infoD', 'a_dictionary':{'of_interest':'item1','not_interesting':'item1a'}},
{'info1':'infoE', 'info2':'infoF', 'stuff_I_want':{'dlist1': [{'of_interest':'item2', 'not_of_interest':'item3'}, {'of_interest':'item4', 'not_of_interest':'item5'}],'dlist2':[{'of_interest':'item6','not_of_interest':'item7','dont_care':'about_this'},{'of_interest':'item8', 'not_interesting':'item9'}]}}]

给予

Traceback (most recent call last):
File "C:\Users\user1\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-426-5b36ee39e1b4>", line 3, in <module>
{'info1':'infoE', 'info2':'infoF', 'stuff_I_want':{'dlist1':[{'of_interest':'item2', 'not_of_interest':'item3'}, {'of_interest':'item4', 'not_of_interest':'item5'}],'dlist2':[{'of_interest':'item6','not_of_interest':'item7','dont_care':'about_this'},{'of_interest':'item8', 'not_interesting':'item9'}]}}]
TypeError: 'type' object has no attribute '__getitem__'

但是:

good_list = [{u'_score': 22.789707, u'symbol': u'RP4-669L17.10', u'_id': u'ENSG00000237094', u'query': u'ENSG00000237094'},
{u'pfam': u'PF03715', u'name': u'NOC2 like nucleolar associated transcriptional repressor', u'_score': 22.789707, u'symbol': u'NOC2L', u'go': {u'CC': [{u'term': u'nucleus', u'pubmed': [16322561, 20959462], u'id': u'GO:0005634', u'evidence': u'IDA'}, {u'term': u'nucleoplasm', u'pubmed': 20123734, u'id': u'GO:0005654', u'evidence': u'IDA'}], u'MF': [{u'term': u'chromatin binding', u'pubmed': [16322561, 20123734], u'id': u'GO:0003682', u'evidence': u'IDA'}, {u'term': u'transcription corepressor activity', u'pubmed': 16322561, u'id': u'GO:0003714', u'evidence': u'IDA'}]}, u'query': u'ENSG00000188976', u'_id': u'26155'},
{u'pfam': u'PF00858', u'name': u'sodium channel epithelial 1 delta subunit', u'_score': 22.79168, u'symbol': u'SCNN1D', u'go': {u'CC': [{u'term': u'plasma membrane', u'id': u'GO:0005886', u'evidence': u'IDA'}, {u'term': u'plasma membrane', u'id': u'GO:0005886', u'evidence': u'TAS'}]}}]

创建没有字典的预期列表。

这两个列表的结构有什么不同使得一个合法而另一个不合法?我觉得我错过了一些愚蠢的东西,但我就是看不到它。

【问题讨论】:

  • 您确定这是重现问题的最小示例吗?您没有包含任何代码,但包含两个列表对象
  • 是的,我只需要运行这两个列表创建。 “good_list = [...]” 按预期创建,“bad_list[...]” 引发错误。我试图了解“bad_list”的坏处
  • 格式化你的bad_list后,我可以声明它完全没问题...repl.it/CqFs
  • 你为什么说bad_list = list[...]而不是bad_list = [...]
  • 哦,这就是问题所在。哎呀 :) 仍然......这不是最小的

标签: python list dictionary nested


【解决方案1】:

您不需要list[] 来构建列表。它不构造列表。它试图从列表的概念中提取一个元素。我认为您的意思是 list(),但这只是更冗长且不太清楚。

[] 从对象中获取项目。 list[] 尝试访问 list 数据类型 中的项目。

list[1] 就像在说“Okey,从列表中获取第一项”。口译员问“哪个列表?”,你回答“列表的概念”。然后解释器以错误响应“列表的概念不是列表”。


深入

说some_object[index] 等同于some_object.__getitem__(index)(语法糖)。所以如果一个数据类型想让你下标([index]),这个数据类型会定义一个__getitem__。

但是类型的类型(是的 - 甚至类型也有类型)不希望您能够自己为类型下标,因此类型类型没有定义 __getitem__。

【讨论】:

    【解决方案2】:

    type' 对象没有属性 'getitem'

    正如错误提示,在第一个中,list[&lt;object&gt;] 部分是这里的问题。

    您正在尝试按对象而不是整数进行索引,因此会出现错误。

    只需剪掉list 部分就可以了。

    例如,下面的一个就可以了。

    bad_list ={'info1':'infoA', 'info2':'infoB'},
    {'info1':'infoC', 'info2':'infoD', 'a_dictionary':{'of_interest':'item1','not_interesting':'item1a'}},
    {'info1':'infoE', 'info2':'infoF', 'stuff_I_want':{'dlist1': [{'of_interest':'item2', 'not_of_interest':'item3'}, {'of_interest':'item4', 'not_of_interest':'item5'}],'dlist2':[{'of_interest':'item6','not_of_interest':'item7','dont_care':'about_this'},{'of_interest':'item8', 'not_interesting':'item9'}]}}
    

    【讨论】:

    • 天哪,我觉得自己很傻。我应该删除这个问题吗?
    • @JamesHanks 没有。这可能会导致问题被禁止。
    • “您尝试按对象而不是整数进行索引,因此出现错误。”这不是核心问题。核心问题是 OP 正在尝试索引列表数据类型
    【解决方案3】:

    bad_list 使用列表函数,但尝试对其下标:

    bad_list = list['h'] 
    Traceback (most recent call last):   File "<stdin>", line 1, \
    in <module> TypeError: 'type' object is not subscriptable
    

    【讨论】:

    • 我想你的意思是list函数?还是函数对象?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多