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

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.

主站蜘蛛池模板: 礼泉县| 客服| 定州市| 张家港市| 梨树县| 黄石市| 永清县| 湖州市| 永吉县| 墨竹工卡县| 台北市| 阜阳市| 确山县| 锦州市| 本溪| 昌都县| 茌平县| 兰西县| 剑河县| 鹤峰县| 贵溪市| 盐津县| 江山市| 秦安县| 嘉兴市| 吉隆县| 丰都县| 平谷区| 武穴市| 花莲市| 杭锦后旗| 呼图壁县| 庆元县| 西昌市| 阿合奇县| 五河县| 和政县| 忻州市| 沙田区| 苏尼特左旗| 彭山县|