Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scala中的数学逻辑删除列表中的特定元素_Scala_List_Math_Logic - Fatal编程技术网

使用scala中的数学逻辑删除列表中的特定元素

使用scala中的数学逻辑删除列表中的特定元素,scala,list,math,logic,Scala,List,Math,Logic,如果我有这个清单: val aList = List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6) 如何通过消除列表第一个标题上的非重复数字来更新列表?因此,结果应该是: valares=List(1,1,1)、List(3,3)、List(7,7,7) 列表(2,3,3,2,6)也应该删除,因为我们没有3在列表的开头。我对结果的期望是: valares=aList(1)map{case List(i)=>List(aL

如果我有这个清单:

val aList =  List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6)
如何通过消除
列表第一个标题上的非重复数字来更新列表?因此,结果应该是:

valares=List(1,1,1)、List(3,3)、List(7,7,7)

列表(2,3,3,2,6)
也应该删除,因为我们没有3在列表的开头。我对结果的期望是:

valares=aList(1)map{case List(i)=>List(aList.groupBy((1))}

但这似乎不符合这种逻辑

除此之外,我还需要将这些结果值转换为另一个列表成员:

val aScore = List(
      /*score for 1*/ List(0, 0, 1500, 2500, 5000),
      /*score for 2*/ List(0, 0, 500, 1000, 2000),
      /*score for 3*/ List(0, 50, 100, 200, 500),
      /*score for 4*/ List(0, 10, 50, 100, 150),
      /*score for 5*/ List(0, 10, 50, 100, 150),
      /*score for 6*/ List(0, 10, 50, 100, 150),
      /*score for 7*/ List(0, 10, 50, 100, 150)
    )
val得分=???

因此,从上面的
aList
结果来看,分数必须是
1500+50+50=1600
,因为
1*3=>1500
3*2=>50
7*3=>50
我的第一个尝试是:

scala> val ls =  List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6))
ls: List[List[Int]] = List(List(1, 1, 1, 3, 4), List(3, 3, 5, 6, 7), List(7, 7, 7, 6, 7), List(2, 3, 3, 2, 6))

scala> ls map { 
  _ groupBy identity filter { case (i, is) => is.length > 1 } flatMap { _._2 } 
}
res2: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7, 7), List(2, 2, 3, 3))
但正如你所看到的,这并不完全是你想要的。我认为下一个解决办法是:

scala> ls map { l => 
         val (h,t) = (l.head, l.tail) 
         h :: t.takeWhile( _ == h ) 
       } filter { _.length > 1 }
res7: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7))
但是请注意,如果
列表。empty
是外部列表的一个元素,那么它将不起作用。

我的第一个尝试是:

scala> val ls =  List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6))
ls: List[List[Int]] = List(List(1, 1, 1, 3, 4), List(3, 3, 5, 6, 7), List(7, 7, 7, 6, 7), List(2, 3, 3, 2, 6))

scala> ls map { 
  _ groupBy identity filter { case (i, is) => is.length > 1 } flatMap { _._2 } 
}
res2: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7, 7), List(2, 2, 3, 3))
object parseData {
  val inputList = List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6))
  val aScore = List(
    /*score for 1*/ List(0, 0, 1500, 2500, 5000),
    /*score for 2*/ List(0, 0, 500, 1000, 2000),
    /*score for 3*/ List(0, 50, 100, 200, 500),
    /*score for 4*/ List(0, 10, 50, 100, 150),
    /*score for 5*/ List(0, 10, 50, 100, 150),
    /*score for 6*/ List(0, 10, 50, 100, 150),
    /*score for 7*/ List(0, 10, 50, 100, 150)
  )

  def isDuplicated(aList: List[Int]): Boolean = aList.head == aList.tail.head

  def getRidOfNonDuplicates(aList: List[Int]): List[Int] = {
    val opList = ListBuffer(aList.head)
    def loop(aList: List[Int], opList: ListBuffer[Int]): Unit = {
      if (aList.tail == Nil) return
      if (aList.head == aList.tail.head) opList += aList.tail.head
      loop(aList.tail, opList)
    }
    loop(aList, opList)
    opList.toList
  }

  def printaScoreValue(aList: List[Int]): Unit = println(aScore(aList.head - 1)(aList.length - 1))

  val outputList = inputList.filter(isDuplicated(_))
  val opList = ListBuffer.empty[List[Int]]
  for (aList <- outputList)
    opList += getRidOfNonDuplicates(aList)
  opList.foreach(printaScoreValue(_))
}
但正如你所看到的,这并不完全是你想要的。我认为下一个解决办法是:

scala> ls map { l => 
         val (h,t) = (l.head, l.tail) 
         h :: t.takeWhile( _ == h ) 
       } filter { _.length > 1 }
res7: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7))

但是请注意,如果
列表不起作用。empty
是外部列表的一个元素。

如果存在重复项,则希望返回某些内容,如果没有,则不返回任何内容,因此请创建一个返回
选项的函数:

object parseData {
  val inputList = List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6))
  val aScore = List(
    /*score for 1*/ List(0, 0, 1500, 2500, 5000),
    /*score for 2*/ List(0, 0, 500, 1000, 2000),
    /*score for 3*/ List(0, 50, 100, 200, 500),
    /*score for 4*/ List(0, 10, 50, 100, 150),
    /*score for 5*/ List(0, 10, 50, 100, 150),
    /*score for 6*/ List(0, 10, 50, 100, 150),
    /*score for 7*/ List(0, 10, 50, 100, 150)
  )

  def isDuplicated(aList: List[Int]): Boolean = aList.head == aList.tail.head

  def getRidOfNonDuplicates(aList: List[Int]): List[Int] = {
    val opList = ListBuffer(aList.head)
    def loop(aList: List[Int], opList: ListBuffer[Int]): Unit = {
      if (aList.tail == Nil) return
      if (aList.head == aList.tail.head) opList += aList.tail.head
      loop(aList.tail, opList)
    }
    loop(aList, opList)
    opList.toList
  }

  def printaScoreValue(aList: List[Int]): Unit = println(aScore(aList.head - 1)(aList.length - 1))

  val outputList = inputList.filter(isDuplicated(_))
  val opList = ListBuffer.empty[List[Int]]
  for (aList <- outputList)
    opList += getRidOfNonDuplicates(aList)
  opList.foreach(printaScoreValue(_))
}
def f(xs: List[Int]) = xs match {
  case x0 :: x1 :: _ if x0 == x1 => Some(xs.takeWhile(_ == x0))
  case _ => None
}
然后
flatMap
将您的列表添加到该列表中,以消除可选位:

aList.flatMap(f)
第二部分:

def getScore(xs: List[Int]) = aScore(xs.head - 1)(xs.size - 1)
因此,只需映射和求和元素。总计:

aList.flatMap(f).map(getScore).sum   
// result = 1600

如果存在重复项,则希望返回某些内容,如果没有,则不希望返回任何内容,因此,请创建一个返回
选项的函数

def f(xs: List[Int]) = xs match {
  case x0 :: x1 :: _ if x0 == x1 => Some(xs.takeWhile(_ == x0))
  case _ => None
}
然后
flatMap
将您的列表添加到该列表中,以消除可选位:

aList.flatMap(f)
第二部分:

def getScore(xs: List[Int]) = aScore(xs.head - 1)(xs.size - 1)
因此,只需映射和求和元素。总计:

aList.flatMap(f).map(getScore).sum   
// result = 1600


你尝试过代码并需要进一步的帮助吗?是的,我尝试过用
x.groupBy
x.splitAt
将它们分开,但这似乎不符合这种逻辑。也许,向你的教授请教一些技巧?@Dima感谢所有的通知,我不会问其他类似的问题,因为我为回答我问题的人感到难过“但我得到了你的-1”。@DedenBangkit我已经看过了你之前提出的十个问题……不要为我投了反对票的人感到难过,他们会好起来的。最好想想你自己。总有一天你会遇到一个问题,你无法从好人那里得到一个解决方案。那么,你打算怎么办?你试过代码和代码了吗?”需要进一步帮助吗?是的,我试着用
x.groupBy
x.splitAt
将它们分开,但这似乎不符合这个逻辑。也许,向你的教授请教一些技巧?@Dima感谢所有的通知,我不会问其他类似的问题,因为我为回答我的问题但从你那里得到-1的人感到难过。@DedenBangkit我已经看了你之前提出的十个问题……不要为我投了反对票的人感到难过,他们会好起来的。最好想想你自己。总有一天你会遇到一个问题,你无法从好人那里得到一个解决方案。那么你打算怎么办?@Dima为什么不标记这个问题,而不是惩罚和阻止一个好的人回答者?我明白你的意思,我不介意我的+1被拿走。让回答者泄气也会带来另一个问题,你不觉得吗?@shaktimaan不,我不介意:)我的目标正是阻止对这样的问题的回答…@Dima我认为,如果我们引导新用户进行这样的相关讨论,会比对尖刻的评论持敌对态度更有帮助。Dima是不对的,正如你链接的元页面所显示的:“询问家庭作业是可以的。”“不要否决那些真诚回答家庭作业问题的人,即使他们违反了这些准则。“.请投我一票@路易吉卜林格右后卫在你面前!:)@迪玛为什么不在这个问题上做标记,而不是惩罚和劝阻回答者?我明白你的意思,我不介意我的+1被拿走。让回答者泄气也会带来另一个问题,你不认为吗?@shaktimaan不,我不:)让回答这样的问题泄气正是我的目标…@Dima我认为如果我们引导新用户进行这样的相关讨论会比对尖刻的评论持敌对态度更有帮助。Dima是不对的,正如你的链接元页面所显示的:“询问家庭作业是可以的。”“不要否决那些真诚地回答家庭作业问题的人,即使他们违反了这些准则。”。请投我一票@路易吉卜林格右后卫在你面前!:)我刚刚意识到我不能为结果放一个,然后把它们全部放平。你的逻辑很好。非常感谢你!您可以将两个
match
表达式简化为一个:def(xs:List[Int])=xs match{case x0::x1::x0==x1=>Some(xs.takeWhile(u==x0))case}@None}@n4to4良好的思维-这肯定更优雅。将更新答案。@DedenBangkit表示元素类型的参数A已经由类
List[A]
BTW定义,我们还可以等价地编写
def(xs:List[Int])=Option(xs)collect{case x0::x1::if x0==x1=>xs.takeWhile(==x0)}
我刚刚意识到,我可以将结果置为零,然后将它们全部放平。你的逻辑很好。非常感谢你!您可以将两个
match
表达式简化为一个:def(xs:List[Int])=xs match{case x0::x1::x0==x1=>Some(xs.takeWhile(u==x0))case}@None}@n4to4良好的思维-这肯定更优雅。将更新答案。@DedenBangkit表示元素类型的参数A已经由类
List[A]
BTW定义,我们还可以等价地编写
def(xs:List[Int])=Option(xs)collect{case x0::x1::ux0==x1=>xs.takeWhile(==x0)}/code>