Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图在列表中创建字典时出现可订阅错误_Python - Fatal编程技术网

Python 试图在列表中创建字典时出现可订阅错误

Python 试图在列表中创建字典时出现可订阅错误,python,Python,在我正在编写的代码中,当我试图将字典编入一个列表时,收到一条错误消息,并将该列表拆分为三个字母的rna密码子。这是我输入的内容: for i in range (0, len(self.codon_dict), 3): #in range from the first object to the last object of the string, in multiples of three codon = (list(self.codon_dict.it

在我正在编写的代码中,当我试图将字典编入一个列表时,收到一条错误消息,并将该列表拆分为三个字母的rna密码子。这是我输入的内容:

for i in range (0, len(self.codon_dict), 3): #in range from the first object to the last         object of the string, in multiples of three
            codon = (list(self.codon_dict.items[i:i+3])) #codons in string are read from      the first object (i) to another object three bases down the string 
            print (codon)
            if codon in NucParams.codon_dict():
                self.codon_dict[codon] +=1
我收到的错误是:

 codon = (list(self.codon_dict.items[i:i+3])) #codons in string are read from the first object (i) to another object three bases down the string
TypeError: 'builtin_function_or_method' object is not subscriptable
他们说一个对象不可下标是什么意思?另外,我怎样才能修复此错误?谢谢


注意:NucParams是我的类,而codon_dict是列出氨基酸编码的三个字母密码子的字典。

首先,您尝试为方法(或函数)项下标,而不是函数的结果;在
self.codon\u dict.items
之后缺少括号
()

其次,假设您像我一样习惯于Python 2,那么您可能会惊讶地发现dict.items()现在返回字典项的“视图”;更多信息,请参阅

下面是一些简单的示例代码,展示了如何在Python 3中使用dict.items()

import itertools

d={'foo':'bar',
   'baz':'quuz',
   'fluml':'sqoob'}

print(list(d.items())[0:2])
print(list(itertools.islice(d.items(),0,2)))
运行此命令会使

[('foo', 'bar'), ('baz', 'quuz')]
[('foo', 'bar'), ('baz', 'quuz')]

如果您使用的是Python2,那么它应该是
codon\u dict.items()[i:i+3]
,否则使用
itertools.islice
。实际上,我使用的是Python3。我以前从未使用过itertools。你能解释一下为什么我要用这个来代替吗?