對不起,新手問題。我嘗試實施蟒蛇issuperset()
使用Scala類如何在Scala中實現Python的issuperset()
Python的例子:
weighted_fruits_set = {"banana", "orange","apple"}
check = {"banana"}
weighted_fruits_set.issuperset(check)
Python的答案:"True"
我的Scala代碼下面,我試圖contains
從的case class weightedFruits
列表中找到superset
我檢查字符串"banana"
存在於weightedFruits.name
中:
object Learn extends App {
case class weightedFruits(name:List[String], weight:Double) {
override def toString = s"name: ${name.mkString("<", ",", ">")}, weight: $weight\n"
}
var weightedFruitsList = new scala.collection.mutable.ListBuffer[weightedFruits]()
weightedFruitsList += (
weightedFruits(List("banana","orange"),180),
weightedFruits(List("banana","orange","apple"),170),
weightedFruits(List("feijoa","fig"),201),
weightedFruits(List("banana","apple","melon"),165)
)
val check = weightedFruits(List("banana"),200)
weightedFruitsList += check
val item = weightedFruitsList(1)
val bol:Boolean = item.name.contains(check.name)
println("item: "+item)
println("check: "+check)
println("bool: "+bol)
}
我的代碼的輸出是false
(但必須是true
):
item: name: <banana,orange,apple>, weight: 170.0
check: name: <banana>, weight: 200.0
bool: false
感謝您的幫助。我真的希望我的解釋是明確
正如我理解你的問題,'issuperset'意思是: def f [A](list1:List [A],list2:List [A]):Boolean'其中'true'表示' list1'包含所有'list2'的元素,否則'false'?所以'f(List(1,2,3),List(1))=== true',但是'f(List(1,2,3),List(4))=== false'? –
'.contains'在你的情況下需要一個元素,而不是一個列表'List(「banana」,「orange」)。contains(List(「banana」))== false' –