- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 679字
- 2021-06-24 14:21:43
Some common functions for arrays
If b = [1:7] and c = [100,200,300], then you can concatenate b and c with the following command:
append!(b, c) #> Now b is [1, 2, 3, 4, 5, 6, 7, 100, 200, 300]
The array, b, is changed by applying this append! method—that's why it ends in an exclamation mark (!). This is a general convention.
Likewise, push! and pop! append one element at the end, or take one away and return that, while the array is changed:
pop!(b) #> 300, b is now [1, 2, 3, 4, 5, 6, 7, 100, 200] push!(b, 42) # b is now [1, 2, 3, 4, 5, 6, 7, 100, 200, 42]
If you want to do the same operations on the front of the array, use popfirst! and pushfirst! (formerly unshift! and shift!, respectively):
popfirst!(b) #> 1, b is now [2, 3, 4, 5, 6, 7, 100, 200, 42] pushfirst!(b, 42) # b is now [42, 2, 3, 4, 5, 6, 7, 100, 200, 42]
To remove an element at a certain index, use the splice! function, as follows:
splice!(b,8) #> 100, b is now [42, 2, 3, 4, 5, 6, 7, 200, 42]
Checking whether an array contains an element is very easy with the in function:
in(42, b) #> true , in(43, b) #> false
To sort an array, use sort! if you want the array to be changed in place, or sort if the original array must stay the same:
sort(b) #> [2,3,4,5,6,7,42,42,200], but b is not changed: println(b) #> [42,2,3,4,5,6,7,200,42] sort!(b) #> println(b) #> b is now changed to [2,3,4,5,6,7,42,42,200]
To loop over an array, you can use a simple for loop:
for e in arr print("$e ") # or process e in another way end
If a dot (.) precedes operators such as + or *, the operation is done element-wise, that is, on the corresponding elements of the arrays:
arr = [1, 2, 3] arr .+ 2 #> [3, 4, 5] arr * 2 #> [2, 10, 6]
As another example, if a1 = [1, 2, 3] and a2 = [4, 5, 6], then a1 .* a2 returns the array [4, 10, 18]. On the other hand, if you want the dot (or scalar) product of vectors, use the LinearAlgebra.dot(a1, a2) function, which returns 32, so this gives the same result as sum(a1 .* a2):
using LinearAlgebra LinearAlgebra.dot(a1, a2) #> 32 sum(a1 .* a2)
Lots of other useful methods exist, such as repeat([1, 2, 3], inner = [2]), which produces [1,1,2,2,3,3].
The methodswith(Array) function returns 47 methods. You can use help in the REPL, or search the documentation for more information.
When you assign an array to another array, and then change the first array, both the arrays change. Consider the following example:
a = [1,2,4,6] a1 = a show(a1) #> [1,2,4,6] a[4] = 0 show(a) #> [1,2,4,0] show(a1) #> [1,2,4,0]
This happens because they point to the same object in memory. If you don't want this, you have to make a copy of the array. Just use b = copy(a) or b = deepcopy(a) if some elements of a are arrays that have to be copied recursively.
As we have seen, arrays are mutable (in contrast to strings), and as arguments to a function, they are passed by reference. As a consequence, the function can change them, as in this example:
a = [1,2,3] function change_array(arr) arr[2] = 25 end change_array(a) println(a) #>[ 1, 25, 3]
Suppose you have an array arr = ['a', 'b', 'c']. Which function on arr do we need to return all characters in one string?
The function join will do the trick: join(arr) returns the string "abc".
string(arr) does not: this returns ['a', 'b', 'c'], but string(arr...) does return "abc". This is because ... is the splice operator (also known as splat). It causes the contents of arr to be passed as individual arguments, rather than passing arr as an array.
- Oracle WebLogic Server 12c:First Look
- Designing Hyper-V Solutions
- Python程序設計案例教程
- jQuery開發基礎教程
- 碼上行動:用ChatGPT學會Python編程
- Hands-On Automation Testing with Java for Beginners
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Keras深度學習實戰
- OpenCV 4計算機視覺項目實戰(原書第2版)
- C#開發案例精粹
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- 計算機應用技能實訓教程
- Mastering HTML5 Forms
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- 從零開始學UI:概念解析、實戰提高、突破規則