我希望对姓氏名称的人名进行分组。
以下内容尚未完成
val names = List("Smith","Adams","Crawford")
val people =List("Billy Jean","Will Morten","Lenhardt Peterson","Lauryn Crawford","George Smith","Priscilla Adams")
people.filter(p=> names.exists(p.name.contains))
答案 0 :(得分:1)
天真而不是最有效的解决方案:
val names = Set("Smith","Adams","Crawford")
val people = List("Billy Jean","Will Morten",
"Lenhardt Peterson","Lauryn Crawford",
"George Smith","Priscilla Adams")
people.filter(x => names.contains(x.split(" ").lastOption.getOrElse("")))
.foreach(println)
结果:
Lauryn Crawford
George Smith
Priscilla Adams
答案 1 :(得分:1)
姓氏有各种令人困惑的配置。
val names = List("Smith","von Braun","Jones","de Kooning")
val people =List("Billy Jean","Will Morten","Lenhardt Peterson",
"Lauryn Crawford","George Smith","Priscilla Adams",
"Wernher von Braun", "Diederik Arnoldus De Beer", "Willem de Kooning")
names.flatMap(n => people.find(_.matches(s".* $n")))
// res0: List[String] = List(George Smith, Wernher von Braun, Willem de Kooning)