- 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]
推薦閱讀
- scikit-learn Cookbook
- 跟“龍哥”學(xué)C語言編程
- R語言數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南
- Flink SQL與DataStream入門、進(jìn)階與實(shí)戰(zhàn)
- 編寫高質(zhì)量代碼:改善C程序代碼的125個(gè)建議
- Mastering Apache Maven 3
- 組態(tài)軟件技術(shù)與應(yīng)用
- Building Machine Learning Systems with Python(Second Edition)
- Mastering Web Application Development with AngularJS
- Learning Hadoop 2
- JavaScript動(dòng)態(tài)網(wǎng)頁編程
- OpenCV with Python Blueprints
- Android移動(dòng)應(yīng)用開發(fā)項(xiàng)目教程
- Software Development on the SAP HANA Platform
- 寫給青少年的人工智能(Python版·微課視頻版)