Julia(未來可能替代Python與R語言) 數組與字典函數

關注微信公共號:小程在線

關注CSDN博客:程志偉的博客

 

1.in

value in X,表示value是否在X中,在X返回True,不在返回false;

julia> x = [58,69,10,125,-85,-98]
6-element Array{Int64,1}:
  58
  69
  10
 125
 -85
 -98

julia> 446 in x
false

julia> 69 in x
true

 

2.append!() 數據的合併

julia> a = ["qwe",451];

julia> b = ["Z",451];

julia> append!(a,b);show(a)
{"qwe",451,"Z",451}

 

3.pop!()

pop!(D,K,defaule):D是字典,K是要選擇的數據,default表示K不存在時要返回的結果

julia> z = Dict("w" => 25,"q"=> true,"a" =>52)
Dict{String,Integer} with 3 entries:
  "w" => 25
  "q" => true
  "a" => 52

julia> pop!(z,"a")
52

julia> pop!(z,"da",-2)
-2

 

4.push!()

它是將一個數組與另外一個元素合併成一個新的數組;

julia> s = [521,"asc",true,-857,'s'];

julia> s
5-element Array{Any,1}:
  521     
     "asc"
 true     
 -857     
  's'     

julia> push!(s,879)
6-element Array{Any,1}:
  521     
     "asc"
 true     
 -857     
  's'     
  879

 

5.splice!()

splice!(A,ind,a):A是一個數組,ind是一個索引,a是需要替換的值

 julia> A = [6, 5, 4, 3, 2, 1]; splice!(A, 5)
  2

  julia> A
  5-element Array{Int64,1}:
   6
   5
   4
   3
   1

  julia> splice!(A, 5, -1)
  1

  julia> A
  5-element Array{Int64,1}:
    6
    5
    4
    3
   -1

  julia> splice!(A, 1, [-1, -2, -3])
  6

  julia> A
  7-element Array{Int64,1}:
   -1
   -2
   -3
    5
    4
    3
   -1

 

6.insert!()

insert!(A,ind,a):A是一個數組,ind是一個索引,a是需要插入的值;

julia> insert!(A,3,789)
6-element Array{Int64,1}:
   -1
   -2

789
   -3
    5
    4
    3
   -1

 

7.sort()和sort!()

sort()表示使用快速排序法對數值型數據進行排序,其他數據類型使用合併排序法;

sort!()表示不保留原來的數據的順序;

julia> show(sort(x))
[-85, -8, 1, 56, 89, 564]
julia> show(x)
[1, 56, 89, 564, -8, -85]
julia> sort!(x);show(x)
[-85, -8, 1, 56, 89, 564]
julia> show(x)
[-85, -8, 1, 56, 89, 564]

sort(x,rev=true):表示對數字進行降序排列
julia> show(sort(x,rev=true))
[564, 89, 56, 1, -8, -85]

sort()可以對字符串排序
julia> show(sort(["asw","wdc","edc","tgb"]))
["asw", "edc", "tgb", "wdc"]

 

8.get()

get(D,K,default):D表示數據組,K表示需要的索引,default表示沒有找到時,返回的索引;

julia>  z = Dict("w" => 25,"q"=> true,"a" =>52)
Dict{String,Integer} with 3 entries:
  "w" => 25
  "q" => true
  "a" => 52

julia> get(z,"w","NOT Find")
25

julia> get(z,"w1","NOT Find")
"NOT Find"

 

9.keys()和values()

keys表示字典中的所有鍵

values()表示字典中的所有值

julia> keys(z)
Base.KeySet for a Dict{String,Integer} with 3 entries. Keys:
  "w"
  "q"
  "a"

julia> values(z)
Base.ValueIterator for a Dict{String,Integer} with 3 entries. Values:
  25
  true
  52

 

10.length()和size()

返回數組的長度以及字符串的長度

julia> length(x)
5

julia> size(x)
(5,)

julia> y = "as dataframe my!"
"as dataframe my!"

julia> length(y)
16

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章