Goodmorning,我对以下功能有疑问:
aminoacids = ['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V']
pair_no_change = ['A', 'K']
original_pairs = [['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'],['D', 'E'], ['S', 'F'], ['B', 'C'], ['Y', 'V'], ['K', 'W']]
fq = 5
def frequency(original_pairs, pair_no_change, fq):
updated_pairs = list(list())
for pair in original_pairs:
if pair != pair_no_change:
pair[0] *= fq
pair[1] *= fq
updated_pairs.append([pair[0], pair[1]])
else:
continue
return updated_pairs
updated_pairs = frequency(original_pairs, pair_no_change, fq)
#print(updated_pairs)
for pair in updated_pairs:
print(pair)
这个函数给出了以下输出:
['DDDDD', 'EEEEE']
['SSSSS', 'FFFFF']
['BBBBB', 'CCCCC']
['YYYYY', 'VVVVV']
['KKKKK', 'WWWWW']
我需要一个输出:
['D','E']
['D','E']
['D','E']
['D','E']
['D','E']
['S','F']
['S','F']
['S','F']
['S','F']
['S','F']
etc.
我认为编写for循环的方式有问题,当我写对[0] * = fq时。 谢谢你的时间和答案!
答案 0 :(得分:1)
你正在通过乘以而不是那个来制作数组,只需追加。 试试这个,
aminoacids = ['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V']
pair_no_change = ['A', 'K']
original_pairs = [['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'],['D', 'E'], ['S', 'F'], ['B', 'C'], ['Y', 'V'], ['K', 'W']]
fq = 5
def frequency(original_pairs, pair_no_change, fq):
updated_pairs = list(list())
for pair in original_pairs:
fq1 = fq
if pair != pair_no_change:
# pair[0] *= fq
# pair[1] *= fq
while fq1 > 0:
updated_pairs.append([pair[0], pair[1]])
fq1 -= 1
else:
continue
return updated_pairs
updated_pairs = frequency(original_pairs, pair_no_change, fq)
#print(updated_pairs)
for pair in updated_pairs:
print(pair)
答案 1 :(得分:1)
你想要这样的东西:
>>> pair_no_change = ['A', 'K']
>>> original_pairs = [['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'],['D', 'E'], ['S', 'F'], ['B', 'C'], ['Y', 'V'], ['K', 'W']]
>>> fq = 5
>>> [[x]*fq for x in original_pairs if "".join(pair_no_change)!="".join(x)]
[[['D', 'E'], ['D', 'E'], ['D', 'E'], ['D', 'E'], ['D', 'E']], [['S', 'F'], ['S', 'F'], ['S', 'F'], ['S', 'F'], ['S', 'F']], [['B', 'C'], ['B', 'C'], ['B', 'C'], ['B', 'C'], ['B', 'C']], [['Y', 'V'], ['Y', 'V'], ['Y', 'V'], ['Y', 'V'], ['Y', 'V']], [['K', 'W'], ['K', 'W'], ['K', 'W'], ['K', 'W'], ['K', 'W']]]
答案 2 :(得分:1)
只需在最后添加另一个for循环。
aminoacids = ['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V']
pair_no_change = ['A', 'K']
original_pairs = [['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'], ['A', 'K'],['D', 'E'], ['S', 'F'], ['B', 'C'], ['Y', 'V'], ['K', 'W']]
fq = 5
def frequency(original_pairs, pair_no_change, fq):
updated_pairs = list(list())
for pair in original_pairs:
if pair != pair_no_change:
pair[0] *= fq
pair[1] *= fq
updated_pairs.append([pair[0], pair[1]])
else:
continue
return updated_pairs
updated_pairs = frequency(original_pairs, pair_no_change, fq)
#print(updated_pairs)
for pair in updated_pairs:
for i ,j in zip(list(pair[0]), list(pair[1])):
print [i,j]
<强>输出:强>
['D', 'E']
['D', 'E']
['D', 'E']
['D', 'E']
['D', 'E']
['S', 'F']
['S', 'F']
['S', 'F']
['S', 'F']
['S', 'F']
['B', 'C']
['B', 'C']
['B', 'C']
['B', 'C']
['B', 'C']
['Y', 'V']
['Y', 'V']
['Y', 'V']
['Y', 'V']
['Y', 'V']
['K', 'W']
['K', 'W']
['K', 'W']
['K', 'W']
['K', 'W']
答案 3 :(得分:1)
再添加一个for循环
def frequency(original_pairs, pair_no_change, fq):
updated_pairs = list(list())
for pair in original_pairs:
if pair != pair_no_change:
for i in range(fq):
updated_pairs.append([pair[0], pair[1]])
else:
continue
return updated_pairs
答案 4 :(得分:1)
您正在使用pair[0] *= fq
。
如果pair[0] = 'Q'
和fq = 3
则结果将是字符串'QQQ'
,您只是将结果推送到updated_pairs。
因此,使用for循环来推入updated_pairs数组。
def frequency(original_pairs, pair_no_change, fq):
updated_pairs = list(list())
for pair in original_pairs:
if pair != pair_no_change:
for i in range(fq):
updated_pairs.append([pair[0], pair[1]])
else:
continue
return updated_pairs