Scala 如何通过索引获取子列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30235050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala How to get sublist by index
提问by Viet Son
I have a List(1 ,2 ,3 ,4 ,5) and trying to get a sublist: List(3, 4) from it by the following way:
我有一个 List(1 ,2 ,3 ,4 ,5) 并试图通过以下方式从中获取一个子列表: List(3, 4) :
List(1 ,2 ,3 ,4 ,5).slice(this.size - 3 , this.size - 1 )
But I got an error
但我有一个错误
error: value size is not a member of object
How can I use "this" parameter in Scala just like in Java. Are there other ways to achieve the goal. Thank you so much.
如何像在 Java 中一样在 Scala 中使用“this”参数。是否有其他方法可以实现目标。太感谢了。
回答by dcastro
You should first declare the list, and then refer to the list using its name list, not this:
您应该首先声明列表,然后使用它的名称引用列表,而list不是this:
val list = List(1 ,2 ,3 ,4 ,5)
list.slice(list.size -3, list.size -1)
If you really want to do this in one line, then use reverse, but it's not very efficient:
如果您真的想在一行中执行此操作,请使用reverse,但效率不高:
List(1 ,2 ,3 ,4 ,5).reverse.slice(1, 3).reverse
By the way, that code wouldn't be valid in Java either. thisrefers to the enclosing object, not to the list.
顺便说一句,该代码在 Java 中也无效。this指的是封闭对象,而不是列表。
回答by Biswanath
If you want last 3 or n elements, you can use takeRight(3)or takeRight(n).
如果需要最后 3 个或 n 个元素,可以使用takeRight(3)或takeRight(n)。
Edit based on the question edit:
根据问题编辑进行编辑:
If you need to not take first fand last lelements then
如果您不需要取第一个f和最后一个l元素,那么
List(1,2,3,....n).drop(f).dropRight(l)
For your case it will be List(1,2,3,4,5).drop(2).dropRight(1)
对于您的情况,它将是 List(1,2,3,4,5).drop(2).dropRight(1)
回答by anquegi
The problem is that the word this is not pointing your List, if you want to take some parameter in the middle of the List use take and takeRigth please read the Scala Docs at the end is a link
问题是 this 不是指向你的 List 这个词,如果你想在 List 中间取一些参数使用 take 和 takeRigth 请阅读最后的 Scala Docs 是一个链接
val my_lst = List(1 ,2 ,3 ,4 ,5)
my_lst.takeRight(3).take(2)
my_lst: List[Int] = List(1, 2, 3, 4, 5)
res0: List[Int] = List(3, 4)
first takeRight with the index counting on right my_lst.size - thepositions, so you must write thepositions and then how many elements you want in your new subList
首先使用右侧 my_lst.size 上的索引计数 - thepositions,因此您必须编写 thepositions,然后在新的 subList 中写入您想要的元素数量
Here is a explanations of how work the methods in scala List
这里解释了 scala List 中的方法是如何工作的
from Scala Help Doc
来自 Scala 帮助文档
def slice(from: Int, until: Int): List[A]
returns
a list containing the elements greater than or equal to index from extending up to (but not including) index until of this list.Definition Classes List → LinearSeqOptimized → IterableLike → TraversableLike → GenTraversableLike Example:
def slice(from: Int, until: Int): List[A]
回报
a list containing the elements greater than or equal to index from extending up to (but not including) index until of this list.定义类列表 → LinearSeqOptimized → IterableLike → TraversableLike → GenTraversableLike 示例:
// Given a list val letters = List('a','b','c','d','e')
// 给定一个列表 val 字母 = List('a','b','c','d','e')
// slicereturns all elements beginning at index fromand afterwards,
// up until index until(excluding index until.)
letters.slice(1,3) // Returns List('b','c')
//slice返回从 index 开始from和之后的所有元素, // 直到 index until(不包括 index until.) letter.slice(1,3) // 返回 List('b','c')
in you case use length or size
在你的情况下使用长度或大小
scala> val my_lst = List(1 ,2 ,3 ,4 ,5)
my_lst: List[Int] = List(1, 2, 3, 4, 5)
scala> my_lst.slice(my_lst.length-3,my_lst.length)
res0: List[Int] = List(3, 4, 5)
but really scala List class has a lot of functions that can fit you. In your case if you want the last 3 elemments use takeRight
但实际上 scala List 类有很多适合您的功能。在您的情况下,如果您想要最后 3 个元素,请使用 takeRight
def takeRight(n: Int): List[A]
Selects last n elements.
n
the number of elements to take returns a list consisting only of the last n elements of this list, or else the whole list, if it has less than n elements.Definition Classes List → IterableLike
def takeRight(n: Int): List[A]
选择最后 n 个元素。
n
the number of elements to take returns a list consisting only of the last n elements of this list, or else the whole list, if it has less than n elements.定义类列表 → IterableLike
scala> my_lst.takeRight(3)
res2: List[Int] = List(3, 4, 5)
Take a look at the Scala Docs
看看 Scala文档
回答by Marcelo Madrazo
You have a List(1, 2, 3, 4, 5)
你有一个 List(1, 2, 3, 4, 5)
1) Is that a Function or Object or a Class?
1) 那是函数、对象还是类?
2) You want to use return values from List( x , x , 3 , 4 , x )
2) 您想使用 List( x , x , 3 , 4 , x ) 的返回值
3) you need to use size outside of the parentheses to return the value from your new List function...
3) 您需要在括号外使用 size 来从新的 List 函数返回值...
4) create a function to complete this step then return it to a variable...
4)创建一个函数来完成这一步,然后将它返回给一个变量......
5) when you fire the function List() use variables with List( A , B , C , D , E )
5) 当您触发函数 List() 时,使用 List( A , B , C , D , E ) 变量
6) all objects must be completely declared before use. for example, How can you use a basketball without air inside? or How can you use a football without being covered in pigskin? or How can you use a computer without a CPU? all components of objects must be completely declared and working properly in programming!
6) 所有对象在使用前必须完整声明。例如,你怎么能在没有空气的情况下使用篮球?或者你怎么能在不被猪皮覆盖的情况下使用足球?或者如何使用没有 CPU 的计算机?对象的所有组件必须完全声明并在编程中正常工作!
7) All data, functions, objects inside your List() function must be working properly or ... functioning . . .
7) List() 函数中的所有数据、函数、对象都必须正常工作或……正常运行。. .
8) You said "In general I also want to get some elements in the middle" ..
8)你说“一般我也想在中间得到一些元素”..
9) As a veteran programmer... You have to know exactly where the elements are in your List() function
9) 作为一个资深程序员......你必须确切地知道元素在你的 List() 函数中的位置
10) Also from the start, before all these functions you wish to "use" you must load the classes, that is filled with all the functions you use, at the head of your program.
10) 同样从一开始,在您希望“使用”所有这些功能之前,您必须在程序的开头加载包含您使用的所有功能的类。
11) you cannot use a function the same way you use an object or a class!
11) 不能像使用对象或类一样使用函数!
12) Lastly, you just want to be creative when the programming language sets the rules or boundaries ...Scala is not Java ...
12) 最后,当编程语言设置规则或界限时,您只想发挥创造力……Scala 不是 Java……
Does this help? with "error: value size is not a member of object"
这有帮助吗?带有“错误:值大小不是对象的成员”

