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

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.

主站蜘蛛池模板: 新密市| 常德市| 文昌市| 大理市| 承德县| 马山县| 罗平县| 阜南县| 资中县| 敦化市| 盐亭县| 育儿| 瑞昌市| 福贡县| 图木舒克市| 武宣县| 博湖县| 抚宁县| 河津市| 钟祥市| 方城县| 离岛区| 即墨市| 乌什县| 嘉荫县| 神池县| 岗巴县| 凉山| 屏边| 尼勒克县| 和政县| 洛南县| 郁南县| 民权县| 连云港市| 巴里| 怀集县| 宜君县| 西充县| 新沂市| 镇巴县|