获取2个列表之间的罕见商品

时间:2020-05-07 16:22:49

标签: python python-3.x list set-difference

我有2个列表'a1''a2':

update test_j
   set j = jsonb_set( -- Replace the '.reviews' array with a concatenation of two arrays,
                      -- one with the unchanged objects and one with the changed ones.
            j
          , '{reviews}'
          ,    jsonb_path_query_array( -- Extract the objects from the '.reviews' array
                                       -- that need _no_ updating.
                                       -- Uses the SQL/JSON Path Language. 
                   j
                 , '$.reviews[*] ? (@.person.first_name != "Jenna" && @.person.first_name != "Yvonne")'
               )
            || array_to_json(ARRAY( -- convert 'set of jsonb' to PostgreSQL array and back
                                    -- ( do not forget the cast from json to jsonb !)
                   select jsonb_set(  -- Update the json data
                              jsonb_path_query( -- Select the objects from the '.reviews' array
                                                -- that need updating
                                                -- (2 names chosen to demonstrate multiple updates 
                                                -- with the original sample data).
                                  j
                                , '$.reviews[*] ? (@.person.first_name == "Jenna" || @.person.first_name == "Yvonne")'
                              )
                            , '{person,first_name}'
                            , '"Vedat"'
                            , true
                          )
                     from test_j
               ))::jsonb
          , true
       )
     ;

我要创建第三个列表,其中包含前两个列表之间的不常见元素,我应该有类似的内容。

//sample data
df.show()
//+-----+-----+------+------+
//| name| type| item1| item2|
//+-----+-----+------+------+
//|apple|fruit|apple1|apple2|
//|beans| vege|beans1|beans2|
//| beef| meat| beef1| beef2|
//| kiwi|fruit| kiwi1| kiwi2|
//| pork| meat| pork1| pork2|
//+-----+-----+------+------+

//using isin function
df.withColumn("prop",when((col("type").isin(Seq("vege","fruit"):_*)),col("item1")).when(col("type") === "meat",col("item2")).otherwise(col("type"))).show()

df.withColumn("prop",when((col("type") === "fruit") ||(col("type") === "vege"),col("item1")).when(col("type") === "meat",col("item2")).
otherwise(col("type"))).
show()
//+-----+-----+------+------+------+
//| name| type| item1| item2|  prop|
//+-----+-----+------+------+------+
//|apple|fruit|apple1|apple2|apple1|
//|beans| vege|beans1|beans2|beans1|
//| beef| meat| beef1| beef2| beef2|
//| kiwi|fruit| kiwi1| kiwi2| kiwi1|
//| pork| meat| pork1| pork2| pork2|
//+-----+-----+------+------+------+

可以应用集合差异来提取第一个列表中不包含第二个元素的元素。

a1 = [[1, 4], [1, 10], [2, 5], [2, 11], [3, 6], [4, 7], [4, 12], [5, 8], [5, 13], 
      [6, 9], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], [6, 13], [8, 14], [9, 15]]

a2 = [[1, 10], [2, 11], [4, 12], [5, 13], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], 
      [6, 13], [8, 14], [9, 15]]

问题在于它适用于2个列表,而不是这种情况下的2个列表。

对于这种情况,是否有有效的解决方案?最好的问候。

2 个答案:

答案 0 :(得分:1)

[x for x in a1 if x not in a2]

答案 1 :(得分:1)

如果您将a1a2定义为元组,那么您的代码将起作用:

a1 = {(1, 4), (1, 10), (2, 5), (2, 11), (3, 6), (4, 7), (4, 12), (5, 8), (5, 13), (6, 9), (7, 14), (8, 15), (2, 10), (3, 11), (5, 12), (6, 13), (8, 14), (9, 15)}
a2 = {(1, 10), (2, 11), (4, 12), (5, 13), (7, 14), (8, 15), (2, 10), (3, 11), (5, 12), (6, 13), (8, 14), (9, 15)}
a3 = a1.difference(a2)
print(a3)