我在Scala中有一个列表:
val hdtList = hdt.split(",").toList
hdtList.foreach(println)
Output:
forecast_id bigint,period_year bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_system_name string,source_record_type string,gl_source_name string,gl_source_system_name string,year string,period string
有一个从数据帧获取的数组,并将其列转换为数组,如下所示:
val partition_columns = spColsDF.select("partition_columns").collect.flatMap(x => x.getAs[String](0).split(","))
partition_columns.foreach(println)
Output:
source_system_name
period_year
有没有一种方法可以将source_system_name string, period_year bigint
中的元素hdtList
与数组partition_columns
中的元素进行比较,然后将它们放入新的List中,从而从DataRequired()
中过滤掉它们。
我对正确地在正确的集合上应用过滤器/地图并进行比较感到困惑。
谁能让我知道我该如何实现?
答案 0 :(得分:2)
除非我对这个问题有误解,否则我认为这是您需要的:
val filtered = hdtList.filter { x =>
!partition_columns.exists { col => x.startsWith(col) }
}
答案 1 :(得分:2)
在这种情况下,您需要使用过滤器,因为您需要从hdtList中删除元素。
Map是一种转换元素的函数,无法使用map从集合中删除元素。如果具有X元素列表,则在执行映射后,您将拥有X元素,不少于多。
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Type.Bool
data Uniq = Uniq deriving Show
data NUniq = NUniq deriving Show
data UUniq = UUniq deriving Show
type family IsMark a :: Bool where
IsMark Uniq = 'True
IsMark NUniq = 'True
IsMark UUniq = 'True
IsMark a = 'False
type Id x = x
type Fn x y = x -> y
type family TL2Fun xs where
TL2Fun '[x] = Id x
TL2Fun (x : xs) = Fn x (TL2Fun xs)
f1 :: TL2Fun '[Int, Int, Int]
f1 a b = a + b
type family WOMarks xs where
WOMarks '[] = '[]
WOMarks (x : xs) = If (IsMark x) (WOMarks xs) (x : (WOMarks xs))
f2 :: TL2Fun (WOMarks '[Int, Int, Int, Uniq])
f2 a b = a + b
请注意,两个列表之间的组合filter +存在是一种算法NxM。如果您的列表很大,您将遇到性能问题。
解决该问题的一种方法是使用Sets。
答案 2 :(得分:1)
同时具有两个列表可能是有用的:partition_columns
中引用的hdt元素和未列出的hdt元素。
val (pc
,notPc) = hdtList.partition( w =>
partition_columns.contains(w.takeWhile(_!=' ')))
//pc: List[String] = List(period_year bigint, source_system_name string)
//notPc: List[String] = List(forecast_id bigint, period_num bigint, ... etc.