Python 如何修复这个程序中的TypeError(列表索引必须是整数,而不是str)?

Python 如何修复这个程序中的TypeError(列表索引必须是整数,而不是str)?,python,list,dictionary,typeerror,indices,Python,List,Dictionary,Typeerror,Indices,在我正在使用的一个程序中,当我试图从dict type数据中挖掘/解析一个值时,我得到一个TypeError 下面是该python程序的简要部分: for c in results: print('value of c : ', c, type(c)) # what I added to see the issue with dict data for ci, lr in c['chain_info'].iteritems(): outfi

在我正在使用的一个程序中,当我试图从
dict type
数据中挖掘/解析一个值时,我得到一个
TypeError

下面是该python程序的简要部分:

for c in results:
        print('value of c : ', c, type(c)) # what I added to see the issue with dict data
        for ci, lr in c['chain_info'].iteritems():
            outfile = open(lr.output_file, 'a')
            write = outfile.write

            if lr.number_vcf_lines > 0:
                write(CHAIN_STRING.format(CHAIN_STRING,
                            from_chr=c['chrom'], from_length=lr.chromosome_length,
                            from_start=0, from_end=lr.chromosome_length,
                            to_chr=c['chrom'], to_length=lr.end_length,
                            to_start=0, to_end=lr.sums[0] + lr.last_fragment_size + lr.sums[2], id=c['chrom']))
                write("\n")
完全回溯错误消息

错误类型:
错误值:列表索引必须是整数,而不是str
/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools/vcf2chain.py:456
回溯(最近一次呼叫最后一次):
文件“/home/everestial007/anaconda3/envs/g2gtools/bin/g2gtools”,第4行,在
__导入\('pkg \'u资源')。运行\'u脚本('g2gtools==0.1.31','g2gtools')
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/setuptools-27.2.0-py2.7.egg/pkg_resources/______.py”,第744行,在运行脚本中
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/setuptools-27.2.0-py2.7.egg/pkg_resources/______.py”,第1499行,在运行脚本中
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools-0.1.31-py2.7.egg info/scripts/g2gtools”,第117行
G2GToolsApp()
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools-0.1.31-py2.7.egg info/scripts/g2gtools”,第75行,在__
getattr(self,args.command)()
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools-0.1.31-py2.7.egg info/scripts/g2gtools”,第90行,在vcf2链中
g2gtools.g2g_commands.command_vcf2chain(sys.argv[2:],self.script_name+'vcf2chain'))
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools/g2g_commands.py”,第375行,在命令\u vcf2链中
vcf2chain(args.input、args.fasta、args.train、args.output、args.keep、args.passed、args.quality、args.diploid)
文件“/home/everestial007/anaconda3/envs/g2gtools/lib/python2.7/site packages/g2gtools/vcf2chain.py”,第493行,在vcf2chain中
引发G2G错误(“执行已暂停”)
g2gtools.exceptions.G2G错误:执行已暂停
文件的github位置:

看起来问题出在线路上:

from_chr=c['chrom'], from_length=lr.chromosome_length,
而且,Chain_字符串是作者创建的:

CHAIN_STRING = "chain 1000 {from_chr} {from_length} + {from_start} {from_end} " + "{to_chr} {to_length} + {to_start} {to_end} {id}"
我试着打印那个dict的值(通过添加一个print语句),看看问题出在哪里

print('value of c : ', c, type(c)) # gave me
('value of c : ', {'chrom': '1', 'stats': OrderedDict([('ACCEPTED', 0)]), 'chain_info': {'right': <g2gtools.vcf2chain.VCFtoChainInfo object at 0x7fe9de396dd0>, 'left': <g2gtools.vcf2chain.VCFtoChainInfo object at 0x7fe9d9252d50>}}, <type 'dict'>)
print('c:'的值,c,type(c))#给了我
('value of c:',{'chrom':'1','stats':OrderedDict([('ACCEPTED',0)],'chain_info':{'right':,'left':},)
那么,这里的问题是什么?
这个数据/程序有什么问题???

在你的评论中,你说
c
是一个字符串,然后
from_chr=c['chrom']
不起作用,因为您不能为字符串指定作为字符串的索引

In [3]: c = 'my string'

In [4]: c['chrom']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-a201470ce4c5> in <module>()
----> 1 c['chrom']

TypeError: string indices must be integers
[3]中的
:c='my string'
在[4]中:c['chrom']
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 c[‘色度’]
TypeError:字符串索引必须是整数

您的错误消息说
列表索引必须是整数,而不是str
。我猜
c
就是一个列表。在这两种情况下,都不能给
c
一个字符串作为索引。

问题是,在for循环过程中,
变量c
作为列表返回:

for c in lr.chain_entries:
   write("\t".join(map(str, c)))
   write("\n") 

当读取另一行时,它将
c
作为列表返回。我不得不挖掘
c['chrom']
希望它是一个
dict类型
。但是,这个变量被forloop更改,导致了所有的麻烦。

完整的回溯会很有用。@glibdud:我以为人们会要求完整的回溯,但有时长问题不会引起注意。我只是放了完整的回溯。我建议在代码后面加上完整的回溯。在代码后面加上完整的回溯。什么是CHAIN_STRING。如果它是一个字符串,并且您正在使用它的
.format
方法,您是否有意将其自身传递回原始字符串?c是
dict type
在结果中c的起始循环
中,但在write语句中(在
If lr.number\u vcf\u lines>0:
之后)。这是怎么可能的。
c
在哪里更改了它的类型?我尝试了
try,除了TypeError
来测试它,在这个程序中似乎就是这样。这很奇怪,我也很惊讶。看起来像是c['chain_info']中的第二个嵌套for循环
,lr。iteritems()
更改了
c
的值和类型。