python - 我想从另一组数字中删除一组数字并打印结果
问题描述
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)
我为这个花了一整天的时间而努力奋斗,但仍然无法解决这个问题。
解决方案
你快到了。
由于您需要从 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));
推荐阅读
- angular - 为什么日期在一天前带有价值?
- ruby - Ruby:字符串插值在第二次调用时首先打印函数
- azure - 如何在保留主 VM 的同时在 Azure 中创建 Azure Windows 映像?
- node.js - 如何将无头铬模块与 AWS Lambda 捆绑在一起?
- google-app-engine - Google App Engine 实例在标准环境中突然关闭
- javascript - 如何识别正在使用哪个元素滚动?
- c# - 两个几乎相同的功能,一个有效,另一个无效?
- powershell - 如何使用变量在 New-DynamicDistributionGroup 上设置 -RecipientFilter
- json - 根据 SQL 表中存在的 JSON 字符串展开表
- python - 在不停止池多处理的情况下对共享阵列进行平等测试