I am not very experienced with Python and my script gives the following error:
我对Python不是很有经验,我的脚本会出现以下错误:
"invalid syntax in line 4"
I don't see the reason, maybe someone can help.
我没有看到原因,也许有人可以提供帮助。
I'm trying to create a list of all possible modifications of a sequence ( like SEQWKST
). If the letter is "S"
, the modification would be "phos"
. If the letter is "K"
there are three possible modifications: "dimeth"
"meth"
and "ac"
我正在尝试创建一个序列的所有可能修改的列表(如SEQWKST)。如果字母是“S”,则修改将是“phos”。如果字母是“K”,则有三种可能的修改:“dimeth”“meth”和“ac”
I want to create a list with the possible modifications and their positions
我想创建一个包含可能的修改和位置的列表
(eg:
1,phos
4,ac
4,meth
4,meth...)
#!/bin/python
def createPTM (SEQWKST):
PTMlist = []
pos = 0
for N in SEQWKST:
pos += 1
if N == "Y":
PTM = phos
PTM_list.extend(pos,PTM)
elif N=="S":
PTM = phos
PTMlist.extend(pos,PTM)
elif N=="T":
PTM = phos
PTMlist.extend(pos,PTM)
elif N=="R":
PTM = meth
PTM_2 = dimeth
PTMlist.extend(pos,PTM)
PTMlist.extend(pos,PTM_2)
elif N=="K":
PTM = meth
PTM_2 = dimeth
PTM_3 = ac
PTMlist.extend(pos,PTM)
PTMlist.extend(pos,PTM_2)
PTMlist.extend(pos,PTM_3)
return PTM_list
print PTM_list
1 个解决方案
#1
2
There are, in fact, several problems with your code, but a SyntaxError at line 4 doesn't appear to be one of them.
事实上,您的代码存在一些问题,但第4行的SyntaxError似乎不是其中之一。
Note that extend
takes a single argument (a sequence of some kind) and extends a given list with that sequence, so you need to call it with e.g. PTM_list.extend((pos,PTM))
请注意,extend接受一个参数(某种序列)并使用该序列扩展给定列表,因此您需要使用例如PTM_list.extend((POS,PTM))
You don't define phos
, meth
, etc, but I'm guessing these are supposed to be string objects?
你没有定义phos,meth等,但我猜这些应该是字符串对象?
You print PTM_list
outside your function definition, where it is no longer in scope. I'm guessing you want to print the returned value of create_PTM
您在函数定义之外打印PTM_list,它不再在范围内。我猜你要打印create_PTM的返回值
Try:
尝试:
#!/bin/python
def createPTM (SEQWKST):
PTM_list = []
pos = 0
for N in SEQWKST:
pos += 1
if N == "Y":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="S":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="T":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="R":
PTM = 'meth'
PTM_2 = 'dimeth'
PTM_list.extend((pos,PTM))
PTM_list.extend((pos,PTM_2))
elif N=="K":
PTM = 'meth'
PTM_2 = 'dimeth'
PTM_3 = 'ac'
PTM_list.extend((pos,PTM))
PTM_list.extend((pos,PTM_2))
PTM_list.extend((pos,PTM_3))
return PTM_list
print createPTM('YSRKT')
A more Pythonic approach is to use a dictionary instead of if..elif
and define a function to do your specialized list extending:
更Pythonic的方法是使用字典而不是if..elif并定义一个函数来进行专门的列表扩展:
#!/bin/python
def createPTM (SEQWKST):
PTM_list = []
def extend_PTM_list(pos, PTMs):
for PTM in PTMs:
PTM_list.extend((pos, PTM))
d = {'Y': ['phos'],
'S': ['phos'],
'T': ['phos'],
'R': ['meth', 'dimeth'],
'K': ['meth', 'dimeth', 'ac']
}
for pos, N in enumerate(SEQWKST, start=1):
extend_PTM_list(pos, d[N])
return PTM_list
print createPTM('YSRKT')
Output (eg):
输出(例如):
[1, 'phos', 2, 'phos', 3, 'meth', 3, 'dimeth', 4, 'meth', 4, 'dimeth', 4, 'ac', 5, 'phos']
#1
2
There are, in fact, several problems with your code, but a SyntaxError at line 4 doesn't appear to be one of them.
事实上,您的代码存在一些问题,但第4行的SyntaxError似乎不是其中之一。
Note that extend
takes a single argument (a sequence of some kind) and extends a given list with that sequence, so you need to call it with e.g. PTM_list.extend((pos,PTM))
请注意,extend接受一个参数(某种序列)并使用该序列扩展给定列表,因此您需要使用例如PTM_list.extend((POS,PTM))
You don't define phos
, meth
, etc, but I'm guessing these are supposed to be string objects?
你没有定义phos,meth等,但我猜这些应该是字符串对象?
You print PTM_list
outside your function definition, where it is no longer in scope. I'm guessing you want to print the returned value of create_PTM
您在函数定义之外打印PTM_list,它不再在范围内。我猜你要打印create_PTM的返回值
Try:
尝试:
#!/bin/python
def createPTM (SEQWKST):
PTM_list = []
pos = 0
for N in SEQWKST:
pos += 1
if N == "Y":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="S":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="T":
PTM = 'phos'
PTM_list.extend((pos,PTM))
elif N=="R":
PTM = 'meth'
PTM_2 = 'dimeth'
PTM_list.extend((pos,PTM))
PTM_list.extend((pos,PTM_2))
elif N=="K":
PTM = 'meth'
PTM_2 = 'dimeth'
PTM_3 = 'ac'
PTM_list.extend((pos,PTM))
PTM_list.extend((pos,PTM_2))
PTM_list.extend((pos,PTM_3))
return PTM_list
print createPTM('YSRKT')
A more Pythonic approach is to use a dictionary instead of if..elif
and define a function to do your specialized list extending:
更Pythonic的方法是使用字典而不是if..elif并定义一个函数来进行专门的列表扩展:
#!/bin/python
def createPTM (SEQWKST):
PTM_list = []
def extend_PTM_list(pos, PTMs):
for PTM in PTMs:
PTM_list.extend((pos, PTM))
d = {'Y': ['phos'],
'S': ['phos'],
'T': ['phos'],
'R': ['meth', 'dimeth'],
'K': ['meth', 'dimeth', 'ac']
}
for pos, N in enumerate(SEQWKST, start=1):
extend_PTM_list(pos, d[N])
return PTM_list
print createPTM('YSRKT')
Output (eg):
输出(例如):
[1, 'phos', 2, 'phos', 3, 'meth', 3, 'dimeth', 4, 'meth', 4, 'dimeth', 4, 'ac', 5, 'phos']