我必须通过不在标题中的 lines[0]
项目的索引来删除行列表中的项目。
输入内容如下:
headers = ['internal_id', 'default_code', 'ean13', 'supplier_id', 'product_qty']
lines = [['default_code', 'fld_code', 'test'],[1212, 4545, 'test1'],[45, 787, 'test2']]
预期输出如下:
lines = [['default_code'],[1212],[45]]
到目前为止我尝试做的事情在这里:
for x in lines[0]:
if x not in headers:
for line in lines[0]:
line.pop(line.index(x))
print lines
这没有产生所需的输出。请帮忙解决这个问题。
最佳答案
我根据您的代码进行的更正:
for i, x in reversed(list(enumerate(lines[0]))):
if x not in headers:
for line in lines:
line.pop(i)
print lines
输出:
[['default_code'], [1212], [45]]
关于Python:按索引弹出列表列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36762017/