- Practical Data Wrangling
- Allan Visochek
- 376字
- 2021-07-02 15:16:10
Arrays (lists, if you ask Python)
An array is an ordered collection of values. Each item in an array is called an element. In Python, there can be an indefinite number of elements in an array. Python has a special name for its implementation of arrays, called a list. With regard to Python, I will use both words to refer to the same thing.
The following syntax can be used to create an array:
<arrayName>=[<element1>,<element2>,<element3>,...]
Inside square brackets, each element in the array is listed, separated by commas:
>> myArray = ["foo","bar","sup?","yo!"]
>> print(myArray)
The elements of an array can be accessed by their relative position, or index, in the array. To access an element of an array, you can use the following syntax:
>> <arrayName>[<index>]
For example:
>> print(myArray[0])
>> thirdElement = myArray[2]
>> print(thirdElement)
You may have noticed that the index of the third element of the array is 2, and not 3. This is because indexing starts with zero in Python and many other languages. This can be a bit confusing when starting out. When indexing arrays, be sure to access the nth element of an array using the number, n-1. Also notice that the index of the array has an integer data type. If you try to access an array with a float value, it will cause an error even if it represents an integer number (that is, 1.0).
You can also set the value of a particular element of an array by assigning a value to its index. The following line will set the first element of myArray to the string, I'm a new value:
>> myArray[0] = "I'm a new value"
>> print myArray[0]
You can only access or set a value of an array if the index belongs to the array. The following two lines will generate an error, because the array only has four elements, so its maximum index is three (for the fourth element):
>> myArray[6]
>> myArray[4]
Arrays are useful to represent grouped collections of data like a row in a spreadsheet. By keeping track of a set of data items in an ordered list, it is possible to easily store and retrieve items from a grouped collection of data.
- Internet接入·網(wǎng)絡(luò)安全
- 現(xiàn)代測(cè)控電子技術(shù)
- 構(gòu)建高質(zhì)量的C#代碼
- Learning Microsoft Azure Storage
- Spark編程基礎(chǔ)(Scala版)
- 極簡(jiǎn)AI入門:一本書讀懂人工智能思維與應(yīng)用
- Java開發(fā)技術(shù)全程指南
- STM32G4入門與電機(jī)控制實(shí)戰(zhàn):基于X-CUBE-MCSDK的無刷直流電機(jī)與永磁同步電機(jī)控制實(shí)現(xiàn)
- Photoshop CS3特效處理融會(huì)貫通
- 高維聚類知識(shí)發(fā)現(xiàn)關(guān)鍵技術(shù)研究及應(yīng)用
- 完全掌握AutoCAD 2008中文版:機(jī)械篇
- Python:Data Analytics and Visualization
- 邊緣智能:關(guān)鍵技術(shù)與落地實(shí)踐
- 水晶石影視動(dòng)畫精粹:After Effects & Nuke 影視后期合成
- Data Analysis with R(Second Edition)