我想对Python列表的值进行编码以优化它,但是在函数中间缺少该值。
我的环境如下。
PC 1-Windows 10(64位)不带GPU,Python 3.6.8(Anaconda),PyCharm 2018.1。
PC 2-具有GPU,Python 3.6.8(Anaconda),PyCharm 2019.1。的Windows 10(64位)
我想从“ enzyme.txt”文件中获取蛋白质序列信息,并将字符串数据转换为整数类型。但是,由于序列是一个字符串,因此在更改为整数时,我创建了一个函数来创建和转换类似于代码的字典表。但是,我不知道是什么原因,但是当i = 860,x [i] [j]中的j = 106时没有任何值。因此,for循环因以下错误而停止。
import numpy as np
from keras.utils import np_utils
file = 'enzyme.txt'
def data(file):
f = open(file, 'r')
lines = f.readlines()
seq = []
ec = []
for i in range(0, len(lines)):
lines[i] = lines[i].strip('\n')
seq.append(lines[i][:-2])
ec.append(lines[i][-1])
f.close()
return seq, ec
x, y = data(file)
Amino_Acid_Scalar = {
'X': 0,
'A': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7,
'I': 8,
'K': 9,
'L': 10,
'M': 11,
'N': 12,
'P': 13,
'Q': 14,
'R': 15,
'S': 16,
'T': 17,
'V': 18,
'W': 19,
'Y': 20
}
def amino_acid_to_scalar(amino_acid):
if not amino_acid in Amino_Acid_Scalar:
return None
return Amino_Acid_Scalar[amino_acid]
def sequence_to_scalar(sequence):
scalar = [amino_acid_to_scalar(amino_acid) for amino_acid in sequence]
if None in scalar:
return None
return scalar
def sequences_to_scalar(sequences):
scalars = [sequence_to_scalar(sequence) for sequence in sequences]
return scalars
x = sequences_to_scalar(x)
for i in range(0, len(x)):
for j in range(0, len(x[i])):
#print(x[i][j], i, j)
#tmp = x[i][j]
#print(tmp)
#arr[i][j] = tmp
pass
y = np_utils.to_categorical(y, 7)
x = np.array(x)
y = np.array(y, dtype='int64')
在“ enzyme.txt”文件中,列858至862如下。
ATKAVCVLKGDGPVQGIINFEQKESNGPVKVWGSIKGLTEGLHGFHVHEFGDNTAGCTSAGPHFNPLSRKHGGPKDEERHVGDLRNVTADKDGVADVSIEDSVISLSGDHCIIGRTLVVHEKADDLGKGGNEESTKTGNAGSRLACGVIGIAQ,1
ATKAVCVLKGDGPVQGIINFEQKESNGPVKVWGSIKGLTEGLHGFHVHEFGDNTAGCTSAGPHFNPLSRKHGGPKDEERHVGDLRNVTADKDGVADVSIEDSVISLSGDHCIIGRTLVVHEKADDLGKGGNEESTKTGNAGSRLACGVIGIAQ,1
MRVVVIGAGVIGLSTALCIHERYHSVLQPLDIKVYADRFTPLTTTDVAAGLWQPYLSDPNNPQEADWSQQTFDYLLSHVHGCALEAAKLFGRILEEKKLSRMPPSHL,1
MPKFYCDYCDTYLTHDSPSVRKTHCSGRKHKENVKDYYCKWMEEQAQSLIDKTTAAFQQGKIPPTPFSAPPPAGAMIUGGGAAACUCGACUGCAUAAUUUGUGGUAGUGGGGGACUGCGUUCGCGCUUUCCCCUG,1
GPHMSIHSGRIAAVHNVPLSVLIRPLPSVLDPAKVQSLVDTIREDPDSVPPIDVLWIKGAQGGDYFYSFGGSHRYAAYQQLQRETIPAKLVQSTLSDLRVYLGASTPDLQ,1
显示以下错误。
Using TensorFlow backend.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydev_run_in_console.py", line 53, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Inyong/Documents/PycharmProjects/Test/Test_4_TXT.py", line 81, in <module>
for j in range(0, len(x[i])):
TypeError: object of type 'NoneType' has no len()
所以当我尝试查看停止位置的值时,
> x[860][106]
我收到以下错误消息。
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
非常感谢您的帮助。
答案 0 :(得分:0)
您很好地找到了问题所在。您看到的是x = None
或x[860]=None
,因为您不能拥有None[186]
。
我对整个代码的建议:
len(thing)
或range(len(thing))
上进行迭代的习惯。因为错误很难被追踪,所以不是:)。观看关于https://www.youtube.com/watch?v=EnSu9hHGq5o ord(base)-65
这样的映射,这样您的映射会更容易并且此代码可以缩短。
def amino_acid_to_scalar(amino_acid):
if not amino_acid in Amino_Acid_Scalar:
return None
return Amino_Acid_Scalar[amino_acid]
您可以使用字典“ get”方法替换此功能:
Amino_Acid_Scalar.get(amino_acid, None)
其中“无”是要在没有密钥的情况下发送回的默认值。 (或者您可以使用缩短的版本Amino_Acid_scalar.get(amino_acid)
,因为默认值是None)
import numpy as np
from keras.utils import np_utils
Amino_Acid_Scalar = {
'X': 0,
'A': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7,
'I': 8,
'K': 9,
'L': 10,
'M': 11,
'N': 12,
'P': 13,
'Q': 14,
'R': 15,
'S': 16,
'T': 17,
'V': 18,
'W': 19,
'Y': 20
}
file = 'enzyme.txt'
seqs = []
ecs = []
with open(file, 'r') as f:
for line in f:
try:
seq, ec=line.strip().partition(',')[0:3:2]
seqs.append(seq)
ecs.append(ec)
except (ValueError, IndexError) as e:
print(f'problem was at line {line} with error: {e}')
def sequence_to_scalar(sequence):
for amino_acid in sequence:
value = Amino_Acid_Scalar.get(amino_acid, None)
if value:
yield value
def sequences_to_scalar(sequences):
scalars = [sequence_to_scalar(sequence) for sequence in sequences]
return scalars
scalar_seqs = sequences_to_scalar(seqs)
for count, seq in enumerate(scalar_seqs):
for count_inner, base in enumerate(seq):
print(f'{count}, {count_inner}, {base}')