본문 바로가기

Julia

Julia - 유용한 함수들

rand

rand 함수에 인수를 넣지 않으면 무작위 수를 하나, 인수를 하나만 넣으면 0 ~ 1 사이의 무작위 수가 들어간 Vector가, 그리고 인수를 여러 개 넣으면 인수 개수 만큼의 차원을 가진 Matrix가 나온다.

julia> rand()
0.7160817395988023

julia> rand(3)
3-element Vector{Float64}:
 0.6159810811259436
 0.8036155297309778
 0.05378942118424623

julia> rand(2, 3)
2×3 Matrix{Float64}:
 0.88686   0.591223  0.0904211
 0.866785  0.775144  0.365447

julia> rand(2, 3, 5)
2×3×5 Array{Float64, 3}:
[:, :, 1] =
 0.679872  0.775653  0.25767
 0.767165  0.510194  0.321051

[:, :, 2] =
 0.533612  0.136601  0.1805
 0.312088  0.828945  0.594921

[:, :, 3] =
 0.800129  0.289833    0.53376
 0.336893  0.00733968  0.0938912

[:, :, 4] =
 0.992593  0.959216  0.619049
 0.58212   0.525614  0.170569

[:, :, 5] =
 0.414111  0.551964  0.609787
 0.57231   0.646194  0.338941

 

collect

collect 함수로 iterable 자료형들을 Vector 또는 Matrix로 변환할 수 있다.

julia> collect(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> collect([1,2,3,4,5])
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> collect("abcdefg")
7-element Vector{Char}:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
 'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
 'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
 'g': ASCII/Unicode U+0067 (category Ll: Letter, lowercase)

julia> collect([1 2 3 4; 5 6 7 8])
2×4 Matrix{Int64}:
 1  2  3  4
 5  6  7  8

 

haskey

해당 객체가 딕셔너리의 키 중 하나인지를 알려주는 함수이다.

julia> d1 = Dict("hello" => 1, "bye" => 2)
Dict{String, Int64} with 2 entries:
  "bye"   => 2
  "hello" => 1

julia> haskey(d1, "hello")
true

julia> haskey(d1, "thank you")
false

 

delete!

딕셔너리에서 해당하는 키를 가진 값을 삭제하는 함수이다.

julia> d1 = Dict("hello" => 1, "bye" => 2)
Dict{String, Int64} with 2 entries:
  "bye"   => 2
  "hello" => 1

julia> delete!(d1, "hello")
Dict{String, Int64} with 1 entry:
  "bye" => 2

 

keys

딕셔너리에서 키들의 값을 뽑아내는 함수이다. KeySet을 반환한다.

julia> d1 = Dict("hello" => 1, "bye" => 2)
Dict{String, Int64} with 2 entries:
  "bye"   => 2
  "hello" => 1

julia> keys(d1)
KeySet for a Dict{String, Int64} with 2 entries. Keys:
  "bye"
  "hello"

 

values

딕셔너리에서 밸류들의 값을 뽑아내는 함수이다. ValueIterator를 반환한다.

julia> d1
Dict{String, Int64} with 2 entries:
  "bye"   => 2
  "hello" => 1

julia> values(d1)
ValueIterator for a Dict{String, Int64} with 2 entries. Values:
  2
  1

 

merge

네임드 튜플 또는 딕셔너리들을 병합할 때 사용한다.

julia> t1 = (science = 50, maths = 60)
(science = 50, maths = 60)

julia> t2 = (english = 70, history = 20)
(english = 70, history = 20)

julia> merge(t1, t2)
(science = 50, maths = 60, english = 70, history = 20)

 

julia> d1 = Dict("science" => 50, "maths" => 60)
Dict{String, Int64} with 2 entries:
  "science" => 50
  "maths"   => 60

julia> d2 = Dict("english" => 70, "history" => 20)
Dict{String, Int64} with 2 entries:
  "history" => 20
  "english" => 70

julia> merge(d1, d2)
Dict{String, Int64} with 4 entries:
  "history" => 20
  "science" => 50
  "maths"   => 60
  "english" => 70

 

in, occursin

in은 해당하는 객체가 Iterable 객체에 포함되어 있는지 확인하는 함수이다.

julia> in(1, arr)
true

julia> in("hello", arr)
false

julia> in('h', "hello")
true

julia> s = Set(["cat", "dog", "cow"])
Set{String} with 3 elements:
  "cow"
  "cat"
  "dog"

julia> in("cat", s)
true

julia> in("deer", s)
false

 

스트링의 경우, 해당하는 스트링이 객체에 포함되어 있는지 확인하기 위해서는 occursin 함수를 사용한다.

julia> s1 = "hello world"
"hello world"

julia> occursin("hello", s1)
true

julia> occursin("olw", s1)
false

 

union, intersect, setdiff

합집합, 교집합, 차집합을 반환한다.

julia> s1 = Set(["cat", "dog"])
Set{String} with 2 elements:
  "cat"
  "dog"

julia> s2 = Set(["cow", "deer"])
Set{String} with 2 elements:
  "cow"
  "deer"

julia> s3 = Set(["cow", "sheep", "pig"])
Set{String} with 3 elements:
  "cow"
  "pig"
  "sheep"

julia> intersect(s2, s3)
Set{String} with 1 element:
  "cow"

julia> union(s2, s3)
Set{String} with 4 elements:
  "cow"
  "deer"
  "pig"
  "sheep"

julia> union(s1, s2, s3)
Set{String} with 6 elements:
  "cow"
  "deer"
  "cat"
  "pig"
  "sheep"
  "dog"
  
julia> setdiff(s2, s3)
Set{String} with 1 element:
  "deer"

julia> setdiff(s3, s2)
Set{String} with 2 elements:
  "pig"
  "sheep"

 

length, lastindex

말 그대로 길이를 알려주는 함수. Iterable 한 모든 자료형에서 사용 가능하다.

julia> length("hello")
5

julia> length([1,2,3,4,5])
5

julia> length(Set([1,2,3,4,5]))
5

 

인덱스가 존재하는 자료형에 있어서는 length보다 lastindex가 더 효율적이라고 한다. 인덱스가 존재하지 않는 자료형, 예를 들어 Set 같은 경우에는 사용이 불가능하다.

julia> lastindex("hello")
5

julia> lastindex([1,2,3,4,5])
5

 

split

여타 다른 언어들의 split과 같은 역할을 한다. 인수가 없으면 공백 (" ") 이 기본값으로 되어 split을 수행한다.

julia> s1 = "hello world"
"hello world"

julia> split(s1)
2-element Vector{SubString{String}}:
 "hello"
 "world"

julia> s2 = "cow,cat,dog,sheep"
"cow,cat,dog,sheep"

julia> split(s2, ",")
4-element Vector{SubString{String}}:
 "cow"
 "cat"
 "dog"
 "sheep"

 

parse

문자열을 해당 자료형으로 바꿔준다.

julia> parse(Int64, "10")
10

julia> parse(Float64, "10")
10.0

 

findfirst

처음으로 해당하는 문자열이 등장하는 위치를 알려준다.

julia> s1 = "hello world"
"hello world"

julia> findfirst("world", s1)
7:11

 

replace

해당하는 문자열이 존재한다면 다른 문자열로 바꿔준다.

julia> s1 = "hello world"
"hello world"

julia> replace(s1, "world" => "beom seok")
"hello beom seok"

 

특이하게도 Pair을 사용하여 바꿔준다

 

 

'Julia' 카테고리의 다른 글

Julia - Julia에서 특별한 점들  (0) 2022.02.24
Julia - 코딩 컨벤션  (0) 2022.02.24