- 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.
- Learning NServiceBus(Second Edition)
- Modular Programming with Python
- Hands-On Image Processing with Python
- Java 9 Programming Blueprints
- Learning ASP.NET Core 2.0
- Groovy for Domain:specific Languages(Second Edition)
- Android 應用案例開發大全(第3版)
- Learning Python Design Patterns
- Python算法指南:程序員經典算法分析與實現
- Java編程的邏輯
- 愛上C語言:C KISS
- DB2SQL性能調優秘笈
- Practical Maya Programming with Python
- Isomorphic Go
- Appcelerator Titanium Smartphone App Development Cookbook