티스토리 뷰
Data structures¶
Reference¶
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/intro-to-julia)
Topics:
Series¶
- http://deepstat.tistory.com/45 (01. Getting started)(in English)
- http://deepstat.tistory.com/46 (01. Getting started(한글))
- http://deepstat.tistory.com/47 (02. Strings)(in English)
- http://deepstat.tistory.com/48 (02. Strings(한글))
- http://deepstat.tistory.com/50 (03. Data structures(한글))
Tuples ¶
We can create a tuple by enclosing an ordered collection of enements in ( ).
Syntax:
(item1, item2, ...)
myfavoriteanimals = ("penguins","cats","sugargliders")
We can index into this tuple.
myfavoriteanimals[1]
but since tuples are immutable, we can't update it
myfavoriteanimals[1] = "otters"
NamedTuples¶
As you might guess, NamedTuples are just like Tuples except that each element additionally have a name! They have a special syntax using = inside a tuple:
(name1 = item1, name2 = item2, ...)
myfavoriteanimals = (bird = "penguins", mammal = "cats", marsupial = "sugargliders")
Like regular Tuples, NamedTuples are ordered, so we can retrieve their elements via indexing:
myfavoriteanimals[1]
They also add the special ability to access values by their name:
myfavoriteanimals.bird
The code below does not work.
myfavoriteanimals["bird"]
This works:
myfavoriteanimals[:bird]
Dictionaries ¶
If we have sets of data related to one another, we may choose to store that data in a dictionary. We can create a dictionary using the Dict() function, which we can initialize as an empty dictionary or one storing key, value pairs.
Syntax:
Dict(key1 => value1, key2 => value2, ...)
myphonebook = Dict("Jenny" => "867-5309", "Ghostbusters" => "555-2368")
In this example, each name and number is a "key" and "value" pair. We can grab Jenny's number (a value) using the associated key
myphonebook["Jenny"]
We can add another entry to this dictionary as follows
myphonebook["Kramer"] = "555-FILK"
myphonebook
We can delete Kramer from our contact list - and simultaneously grab his number - by using pop!
pop!(myphonebook, "Kramer")
myphonebook
Unlike tuples and arrays, dictionaries are not ordered. So, we can't index into them.
myphonebook[1]
In the example above, julia thinks you're trying to access a value associated with the key 1.
The code below does not work.
myphonebook.Jenny
Arrays ¶
Unlike tuples, arrays are mutable. Unlike dictionaries, arrays contain ordered collections. We can create an array by enclosing this collection in [ ].
Syntax:
[item1, item2, ...]
For example, we might create an array to keep track of my friends
myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]
The 1 in Array{String,1} means this is a one dimensional vector. An Array{String,2} would be a 2d matrix, etc. The String is the type of each element.
or to store a sequence of numbers
fibonacci = [1, 1, 2, 3, 5, 8, 13]
mixture = [1, 1, 2, 3, "Ted", "Robyn"]
Once we have an array, we can grab individual pieces of data from inside that array by indexing into the array. For example, if we want the third friend listed in myfriends, we write
myfriends[3]
We can use indexing to edit an existing element of an array
myfriends[3] = "Baby Bop"
myfriends
Yes, Julia is 1-based indexing, not 0-based like Python. Wars are faught over lesser issues. I have a friend with the wisdom of Solomon who proposes settling this once and for all with ½ 😃
We can also edit the array by using the push! and pop! functions. push! adds an element to the end of an array and pop! removes the last element of an array.
We can add another number to our fibonnaci sequence
push!(fibonacci, 21)
fibonacci
and then remove it
pop!(fibonacci)
fibonacci
So far I've given examples of only 1D arrays of scalars, but arrays can have an arbitrary number of dimensions and can also store other arrays.
For example, the following are arrays of arrays:
favorites = [["koobideh", "chocolate", "eggs"],["penguins", "cats", "sugargliders"]]
numbers = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
Below is an example of 2D array.
favorites2D = [["koobideh", "chocolate", "eggs"] ["penguins", "cats", "sugargliders"]]
Below are examples of 2D and 3D arrays populated with random values.
rand(4, 3)
rand(4, 3, 2)
Be careful when you want to copy arrays!
fibonacci
somenumbers = fibonacci
somenumbers[1] = 404
fibonacci
Editing somenumbers caused fibonacci to get updated as well!
In the above example, we didn't actually make a copy of fibonacci. We just created a new way to access the entries in the array bound to fibonacci.
If we'd like to make a copy of the array bound to fibonacci, we can use the copy function.
first, restore fibonacci.
fibonacci[1] = 1
fibonacci
somemorenumbers = copy(fibonacci)
somemorenumbers[1] = 404
fibonacci
In this last example, fibonacci was not updated. Therefore we see that the arrays bound to somemorenumbers and fibonacci are distinct.
Exercises ¶
3.1¶
Create an array, a_ray, with the following code:
a_ray = [1, 2, 3]
Add the number 4 to the end of this array and then remove it.
a_ray = [1, 2, 3]
push!(a_ray,4)
pop!(a_ray)
a_ray
@assert a_ray == [1, 2, 3]
?@assert
3.2¶
Try to add "Emergency" as key to myphonebook with the value string(911) with the following code
myphonebook["Emergency"] = 911
Why doesn't this work?
myphonebook
myphonebook["Emergency"] = 911
myphonebook["Emergency"] = string(911)
3.3¶
Create a new dictionary called flexible_phonebook that has Jenny's number stored as an integer and Ghostbusters' number stored as a string with the following code
flexible_phonebook = Dict("Jenny" => 8675309, "Ghostbusters" => "555-2368")
flexible_phonebook = Dict("Jenny" => 8675309, "Ghostbusters" => "555-2368")
@assert flexible_phonebook == Dict("Jenny" => 8675309, "Ghostbusters" => "555-2368")
3.4¶
Why can we add an integer as a value to flexible_phonebook but not myphonebook? How could we have initialized myphonebook so that it would accept integers as values?
flexible_phonebook["Emergency"] = 911
flexible_phonebook
typeof(flexible_phonebook)
typeof(myphonebook)
myphonebook = convert(Dict{Any,Any},myphonebook)
typeof(myphonebook)
myphonebook["Emergency"] = 911
myphonebook
'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글
04. Loops (0) | 2018.09.15 |
---|---|
03. Data structures (한글) (0) | 2018.09.13 |
02. Strings (한글) (0) | 2018.09.12 |
02. Strings (0) | 2018.09.12 |
01. Getting started (한글) (0) | 2018.09.11 |