首页 \ 问答 \ Python列表减法操作(Python list subtraction operation)

Python列表减法操作(Python list subtraction operation)

我想做类似这样的事情:

>>> x = [1,2,3,4,5,6,7,8,9,0]  
>>> x  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]  
>>> y = [1,3,5,7,9]  
>>> y  
[1, 3, 5, 7, 9]  
>>> y - x   # (should return [2,4,6,8,0])

但是这不是python列表的支持什么是最好的方法呢?


I want to do something similar to this:

>>> x = [1,2,3,4,5,6,7,8,9,0]  
>>> x  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]  
>>> y = [1,3,5,7,9]  
>>> y  
[1, 3, 5, 7, 9]  
>>> y - x   # (should return [2,4,6,8,0])

But this is not supported by python lists What is the best way of doing it?


原文:https://stackoverflow.com/questions/3428536
更新时间:2022-06-19 14:06

最满意答案

使用CASE

SELECT 
  TABEL1.Id, 
  CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
       THEN 'TRUE' 
       ELSE 'FALSE'
  END AS NewFiled  
FROM TABLE1

如果TABLE2.ID是唯一或主键,您还可以使用:

SELECT 
  TABEL1.Id, 
  CASE WHEN TABLE2.ID IS NOT NULL
       THEN 'TRUE' 
       ELSE 'FALSE'
  END AS NewFiled  
FROM TABLE1
  LEFT JOIN Table2
    ON TABLE2.ID = TABLE1.ID

Use CASE:

SELECT 
  TABEL1.Id, 
  CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
       THEN 'TRUE' 
       ELSE 'FALSE'
  END AS NewFiled  
FROM TABLE1

If TABLE2.ID is Unique or a Primary Key, you could also use this:

SELECT 
  TABEL1.Id, 
  CASE WHEN TABLE2.ID IS NOT NULL
       THEN 'TRUE' 
       ELSE 'FALSE'
  END AS NewFiled  
FROM TABLE1
  LEFT JOIN Table2
    ON TABLE2.ID = TABLE1.ID

相关问答

更多
  • 假设您希望两个查询都给出相同的结果,您需要在第二个查询的WHERE子句中添加括号以使布尔逻辑匹配: select * from #t1 where exists ( select * from #t2 where #t1.id = #t2.id ) and (#t1.typ = 'stu' or typ = 'exstu') Assuming you want both queries to give the same resul ...
  • 如果您在存储过程中有此查询,则可能由于参数嗅探而发生这种情况。 尝试这样的东西来检查它: declare @ISALLOWED_internal select @ISALLOWED_internal = @ISALLOWED IF EXISTS(SELECT TOP 1 FROM TABLE1 WHERE COLUMN1 = 'XYZ') OR @ISALLOWED_internal = 1 BEGIN -- SQL Statements END If you have this query ...
  • 使用CASE : SELECT TABEL1.Id, CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID) THEN 'TRUE' ELSE 'FALSE' END AS NewFiled FROM TABLE1 如果TABLE2.ID是唯一或主键,您还可以使用: SELECT TABEL1.Id, CASE WHEN TABLE2.ID IS NOT NULL ...
  • 我不太清楚问题是什么,但看起来你需要在if not exists行上添加@: if not EXISTS(select Groups.id from Groups where Groups.name =@GroupName and Groups.userID = @userID) 编辑:实际上看起来你也错过了底部的一个。 SELECT @OutGroupID = (select Groups.id from Groups ...
  • 对于Access SQL,您需要使用如下构造: SELECT ... FROM tblFilter WHERE Criterion1 IN (SELECT Criterion2 FROM tblFilter) OR Criterion1 IN (SELECT Criterion3 FROM tblFilter) For Access SQL you will need to use a construct like this: SELECT ... FROM tblFilter WHER ...
  • 你可以这样做: SELECT TOP 1 UserID FROM UserTable WHERE Name IN (@UserName, 'John Skeet') ORDER BY CASE WHEN Name = 'John Skeet' THEN 2 ELSE 1 END (或使用LIMIT或任何方法为您的RDBMS) 或者你可以这样做: DECLARE @UserID INT SELECT @UserID = UserID FROM U ...
  • 在许多数据库中有子查询时,需要为其指定别名: SELECT CompanyCode, AgentId FROM UniqueAgentIdToUniqueAgentId un WHERE un.UniqueAgentId = (SELECT UniqueAgentId FROM (SELECT q.LastChangeDate, a.UniqueAgentId FROM QueueUpdates q, AgentProductTraining a ...
  • 您可以使用LEFT JOIN而不是EXISTS编写反连接: SELECT t1.* FROM Table1 t1 LEFT JOIN Table2 t2 ON t2.Id = t1.Id WHERE t2.Id IS NULL 但是对于EXISTS运算符, 您必须有一个子查询 。 You could write an anti-join using LEFT JOIN instead of an EXISTS: SELECT t1.* FROM Table1 t1 LEFT JOIN Table ...
  • 我使用WHERE NOT EXISTS错了吗? 是。 你想使用NOT IN而不是NOT EXISTS。 如果使用NOT EXISTS并且非存在子查询返回任何行,则条件将为false,并且主查询不会返回任何数据。 如果没有返回任何行,则条件将为true,并且主查询将返回所有行(因为,在此示例中,主查询中没有其他条件)。 通常,NOT EXISTS中的子查询是相关的子查询,因此必须为每一行评估子查询。 在这里,您没有相关的子查询(这对性能有利)。 但是您的查询意味着“返回有关所有角色的信息,除非在指定用户的最后 ...
  • 这正是left join的用途。 要回答注释中的后续问题,可以使用coalesce替换从左连接返回的null : SELECT orders.id_o, orders.user_id, orders.price, COALESCE(users.username, 'Guest') FROM orders LEFT JOIN users ON orders.user_id = users.id ORDER BY order_date ASC This is exa ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。