Python 替换单引号中的子列表(用逗号分隔)

Python 替换单引号中的子列表(用逗号分隔),python,python-3.x,Python,Python 3.x,使用Python3.x,我有一段代码生成x,子列表列表用逗号分隔…需要打印行Y,替换x中的值 X = ['sequence, A, 1, 2, 3', 'sequence, B, 10, 20, 30', 'sequence, C, 100, 200, 300'] Y = ['VEQ','1','map','2','cap',] 预期产量 'VEQ','1','A','2','1' 'VEQ','1','B','2','10' 'VEQ','1','C','2','1

使用Python3.x,我有一段代码生成
x
,子列表列表用逗号分隔…需要打印行
Y
,替换
x
中的值

X = ['sequence, A, 1, 2, 3', 
     'sequence, B, 10, 20, 30', 
     'sequence, C, 100, 200, 300']

Y = ['VEQ','1','map','2','cap',]
预期产量

'VEQ','1','A','2','1'
'VEQ','1','B','2','10'
'VEQ','1','C','2','100'
Y[field3 map]
替换为
X[field2]
Y[field5 cap]
替换为
X[field3]
Y的其他字段将保持不变

我已经试着根据我的要求修改下面的参考代码,但没有成功

# helper function to find elements  
def find_sub_idx(test_list, repl_list, start = 0): 
    length = len(repl_list) 
    for idx in range(start, len(test_list)): 
        if test_list[idx : idx + length] == repl_list: 
            return idx, idx + length 

# helper function to perform final task 
def replace_sub(test_list, repl_list, new_list): 
    length = len(new_list) 
    idx = 0
    for start, end in iter(lambda: find_sub_idx(test_list, repl_list, idx), None): 
        test_list[start : end] = new_list 
        idx = start + length 

不确定这是否是您想要的,但下面是:

X = ['sequence, A, 1, 2, 3', 
     'sequence, B, 10, 20, 30', 
     'sequence, C, 100, 200, 300']

Y = ['VEQ','1','map','2','cap',]

def replace(X,Y,position_list):
    result = []
    for x in X:
        x = x.split(',')
        x = [t.strip() for t in x]
        temp = Y.copy()
        for pos in position_list:
            temp[pos[0] - 1] = x[pos[1] - 1]
        result.append(temp)
    return result

replace(X,Y,[(3,2),(5,3)])
输出:

[['VEQ', '1', 'A', '2', '1'],
 ['VEQ', '1', 'B', '2', '10'],
 ['VEQ', '1', 'C', '2', '100']]

这里的
position\u list
是要替换的(Y\u pos,X\u pos)元组列表,例如
(3,2)
意味着用
X

的第二个值替换
Y的第三个值,因此,只需一份
Y
,即可覆盖:

X=['序列,A,1,2,3',
'序列,B,10,20,30',
'顺序,C,100200300']
Y=['VEQ','1','map','2','cap',]
retval=Y[:]
对于地图中的x(λx:str.split(x,,,),x):
retval[2]=x[1]
retval[4]=x[2]
打印(返回)
如果要将值存储在某个位置,每次都需要有一份单独的
Y

X=['序列,A,1,2,3',
'序列,B,10,20,30',
'顺序,C,100200300']
Y=['VEQ','1','map','2','cap',]
我的清单=[]
对于地图中的x(λx:str.split(x,,,),x):
retval=Y[:]
retval[2]=x[1]
retval[4]=x[2]
my_list.append(retval)

Y:的附加是什么意思?发布一个显式的预期结果。真是太棒了。。它起作用了。。我试过P=replace(X,Y,[(3,2),(5,3)])P=P.split(“,”)print(P)但是失败了,因为P(list)没有拆分成员。你想把那些列表转换回类似字符串的
'VEQ,1,A,2,1'
?如果是这样,您可以尝试:
P=[','。为P中的项加入(项)]