首页 > 解决方案 > 我想从另一组数字中删除一组数字并打印结果

问题描述

ListA https://docs.google.com/spreadsheets/d/1YwE4wRtua7KTdX9IaRoXo8nRyvueIwzSA0IThJULj1s/edit?usp=sharing

列表 B https://docs.google.com/spreadsheets/d/1-e-fgCQOIG5PMjCp73GO4OcwvKpBuxKTDeH6UCna1fI/edit?usp=sharing

我想从 List A 中删除 List B 元素并打印结果。我怎样才能做到这一点?

我尝试过使用 python 脚本,但它只是将列表 A 或列表 B 作为输出:

import os

with open('/Users/xyz/Desktop/listA.csv') as listA:
        first = listA.read().splitlines()

    with open('/Users/xyz/Desktop/listB.csv') as listB:
        second = listB.read().splitlines()

    bigb = set(first)  - set(second)

    with open('f1.csv', 'w') as f3:
        out = '\n'.join(list(bigb))
        f3.write(out)

我也尝试过使用 SQLite:

SELECT B
FROM listB b
WHERE NOT EXISTS (
    SELECT *
    FROM listA a
    WHERE a.A = b.B)

我为这个花了一整天的时间而努力奋斗,但仍然无法解决这个问题。

标签: pythonsqlite

解决方案


你快到了。

由于您需要从 listA 中删除某些内容,因此您需要从它的名称开始

delete from listA as a
 where exists (select 1 from listB b where b.num_col = a.num_col);

UPD。似乎 listB 中的许多数字都包含需要删除的尾随双引号。我看到了两种方式

1. Update table to get rid of double-quotes at all

update listB
   set num_col = replace(num_col, '"','');

then run the script from above

2. If updating table can't be done, you may use this updated query

 delete from listA as a
 where exists (select 1 from listB b where replace(b.num_col, '"', '') = cast(a.num_col as text));

推荐阅读