官术网_书友最值得收藏!

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.

A function whose name ends in a ! changes its first argument.

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.

主站蜘蛛池模板: 拉萨市| 阿拉善盟| 沧源| 蕲春县| 天等县| 鹤峰县| 东安县| 邹平县| 温州市| 沽源县| 竹溪县| 沭阳县| 昌黎县| 三都| 康定县| 维西| 龙川县| 固镇县| 静海县| 陇西县| 舟山市| 南丹县| 侯马市| 阳谷县| 哈巴河县| 新干县| 桦甸市| 宁都县| 桐梓县| 陈巴尔虎旗| 乌拉特后旗| 浙江省| 佳木斯市| 栾川县| 巴彦淖尔市| 东港市| 额济纳旗| 健康| 长白| 若尔盖县| 平和县|