因此,我有两个列表,需要添加其整数元素。
nested_lst_1 = [[6],[7],[8,9]]
lst = [1,2,3]
我需要添加它们,以便将嵌套列表中的每个元素都添加到其在“ lst”中对应的整数,以获得另一个嵌套列表。
nested_list_2 = [[6 + 1],[7 + 2],[8 + 3,9 + 3]]
或
nested_list_2 = [[7],[9],[11,12]]
然后,我需要使用nested_list_1和nested_list_2中的整数作为索引,以从字符串中提取子字符串。
nested_list_1 = [[6],[7],[8,9]] *obtained above*
nested_list_2 = [[7],[9],[11,12]] *obtained above*
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
string[6:7] = 'CG'
string[7:9] = 'GTA'
string[8:11] = 'TACG'
string[9:12] = 'ACGA'
然后,我需要为获得的子字符串创建一个嵌套列表:
nested_list_substrings = [['CG'],['GTA'],['TACG','ACGA']]
最后,我需要将这些子字符串用作字典中的键值,该字典也具有string类型的键。
keys = ['GG', 'GTT', 'TCGG']
nested_list_substrings = [['CG'],['GTA'],['TACG','ACGA']]
DNA_mutDNA = {'GG':['CG'], 'GTT':['GTA'], 'TCGG':['TACG','ACGA']}
我知道这是一个多步骤问题,但是如果您能以任何方式提供帮助,我都非常感谢。
答案 0 :(得分:2)
假设您不需要中间变量,则可以通过字典理解来完成所有这些操作:
a = [[6],[7],[8,9]]
b = [1,2,3]
keys = ['GG', 'GTT', 'TCGG']
s = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
DNA_mutDNA = {k: [s[start:start+length+1] for start in starts]
for k, starts, length in zip(keys, a, b)}
答案 1 :(得分:1)
您可以直接使用嵌套列表理解来生成子字符串列表,nested_lst_2
不是必需的。
nested_lst_1 = [[6],[7],[8,9]]
lst = [1,2,3]
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
keys = ['GG', 'GTT', 'TCGG']
substrings = [[string[v:i+v+1] for v in u] for i, u in zip(lst, nested_lst_1)]
print(substrings)
DNA_mutDNA = dict(zip(keys, substrings))
print(DNA_mutDNA)
输出
[['CG'], ['GTA'], ['TACG', 'ACGA']]
{'GG': ['CG'], 'GTT': ['GTA'], 'TCGG': ['TACG', 'ACGA']}
答案 2 :(得分:0)
library("lpSolveAPI")
ncol <- nrow(hockey) # of players
nteams <- length(unique(hockey$Team))
teams <- unique(hockey$Team)
lpmodel <- make.lp(ncol=(ncol + nteams))
obj_vals <- hockey[, "Projection"]
set.objfn(lpmodel, c(obj_vals, rep(0, nteams))) #dummy 0s for team variable
lp.control(lpmodel,sense='max')
set.type(lpmodel, columns=1:(ncol+nteams), type = "binary")
add.constraint(lpmodel, xt=c(hockey$Salary, rep(0, nteams)), type="<=", rhs=50000)
add.constraint(lpmodel, xt=c(as.numeric(hockey$Position=="C"), rep(0, nteams)), type="=", rhs=2)
add.constraint(lpmodel, xt=c(as.numeric(hockey$Position=="D"), rep(0, nteams)), type="=", rhs=2)
add.constraint(lpmodel, xt=c(as.numeric(hockey$Position=="W"), rep(0, nteams)), type="=", rhs=4)
add.constraint(lpmodel, xt=c(as.numeric(hockey$Position=="G"), rep(0, nteams)), type="=", rhs=1)
solve(lpmodel)
get.objective(lpmodel)
soln <- get.variables(lpmodel)>0
solution <- hockey[soln[0:ncol],]
print(solution[order(solution$Team),])
答案 3 :(得分:0)
当然不是最易读的方式,这是一些功能样式的乐趣:
nested_lst_1 = [[6], [7], [8,9]]
lst = [1, 2, 3]
nested_lst_2 = list(map(
list,
map(map, map(lambda n: (lambda x: n+x), lst), nested_lst_1)))
nested_lst_2
结果看起来像预期的那样:
[[7], [9], [11, 12]]
然后:
from itertools import starmap
from operator import itemgetter
make_slices = lambda l1, l2: starmap(slice, zip(l1, map(lambda n: n+1, l2)))
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
get_slice = lambda s: itemgetter(s)(string)
nested_list_substrings = list(map(
lambda slices: list(map(get_slice, slices)),
starmap(make_slices, zip(nested_lst_1, nested_lst_2))))
nested_list_substrings
结果:
[['CG'], ['GTA'], ['TACG', 'ACGA']]
最后:
keys = ['GG', 'GTT', 'TCGG']
DNA_mutDNA = dict(zip(keys, nested_list_substrings))
DNA_mutDNA
最终结果:
{'GG': ['CG'], 'GTT': ['GTA'], 'TCGG': ['TACG', 'ACGA']}