- Hands-On Data Science and Python Machine Learning
- Frank Kane
- 168字
- 2021-07-15 17:15:00
Pre colon
If, for example, you want to take the first three elements of a list, everything before element number 3, we can say :3 to get the first three elements, 1, 2, and 3, and if you think about what's going on there, as far as indices go, like in most languages, we start counting from 0. So element 0 is 1, element 1 is 2, and element 2 is 3. Since we're saying we want everything before element 3, that's what we're getting.
So, you know, never forget that in most languages, you start counting at 0 and not 1.
Now this can confuse matters, but in this case, it does make intuitive sense. You can think of that colon as meaning I want everything, I want the first three elements, and I could change that to four just again to make the point that we're actually doing something real here:
x[:4]
The output of the above code example is as follows:
[1, 2, 3, 4]
推薦閱讀
- 計算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- 跟“龍哥”學(xué)C語言編程
- Reactive Programming with Swift
- Functional Programming in JavaScript
- Learning AndEngine
- Expert Data Visualization
- PHP+Ajax+jQuery網(wǎng)站開發(fā)項目式教程
- 0 bug:C/C++商用工程之道
- Android驅(qū)動開發(fā)權(quán)威指南
- Android Studio Cookbook
- PHP 8從入門到精通(視頻教學(xué)版)
- 3D Printing Designs:Octopus Pencil Holder
- Android熱門應(yīng)用開發(fā)詳解
- JavaScript高級程序設(shè)計(第4版)
- JavaScript程序設(shè)計基礎(chǔ)教程(慕課版)