分享

Julia编程05:数据结构(collection)

 生信探索 2023-04-17 发布于云南

vector

Create

Julia的vector与R和Python中的list类似,vector中可以放入任何东西。

v1 = [235"Math"111317]
 
length(v1) #7

1:2:7 #step = 2
 
range(start=1,stop=10,length=10#1.0:1.0:10.0
collect("APPLE") == ['A','P','P','L','E'# string to vector # Vector{Char}
collect((1,3,5)) == [1,3,5]# tuple to vector # Vector{Int64}

其实Julia中的vector只是一维的array,用typeof便能看出来。

typeof([1,3,5])
# Vector{Int64} (alias for Array{Int64, 1})

初始化

zeros(3) == [0.0,0.0,0.0]
zeros(Int64,3) == [0,0,0]

y=Vector{Float64}(undef, 3#3元素,元素值未初始化
fill!(y, 100#填充3个位置
collect(1:5) == [1,2,3,4,5# range to vector

  • 列表生成器

python中也叫列表推导式

[x*x for x in 1:10 if x % 3 == 1]

遍历

# --------------------- eachindex ---------------------
v1 = [134913]
for x in eachindex(v1)
  println("v1[", x, "] = ", v1[x])
end
# --------------------- = ---------------------
for i = 1:length(v1)
    println("v1[", i, "] = ", v1[i])
end
# --------------------- in ---------------------
for x in v1
    println(x)
end
# --------------------- enumerate --------------------
for (i, xi) in enumerate(v1)
    println("v1[", i, "] = ", xi)
end  # 同时对向量下标和元素值遍历

Retrieve

v1[1]
v1[end# 相当于python中的 v1[-1] 
# ---------------------用数组作为下标---------------------
v1[2:4]
v1[:]

v1[2]=0 # 原地修改

# ---------------------用条件作为下标 布尔索引---------------------

v1 = [2357111317]
b = v1 .<= 10
v1[b] == v1[v1 .<= 10]

type

v1 = [1.223.6]
eltype(v1) # 元素类型
typeof(v1) # 容器类型
v1[2] = "abc" # 报错 因为v1是Int64类型

成员判断

8 in 1:8 # true
[3,5] .∈ Ref([1,2,3]) # return 1 0

a=[1,3,5,3]; b=[1,2,3]
indexin(a, b) # 返回向量a的每个元素首次出现在b中的位置, 没有时返回nothing

allunique([1,2,3,3]) # 判断是否元素彼此不同

Delete

pop!(a) # 删除a的最后一个元素
popfirst!(a) # 删除a的第一个元素
splice!(a, [2,3]) # 删除2,3位置上的元素

deleteat!(a, [2,3]) # 无返回值
empty!(a) #清空

Update

# ---------------------增-------------------
push!(a, 2# 将2添加到向量a的末尾
pushfirst!(a, 2# 将2添加到向量a的开头
insert!(a, 32# 将2添加到向量a的第3个位置

append!(a,b) # 将向量b添加到向量a的末尾
vcat(a, b) #合并a,b 无返回值

# ---------------------改-------------------
replace!(b, 1 => 04 => 3, count = 2#替换,count替换次数上限


filter

# ---------------------筛选-------------------
v3 = [2233511]
#删除不满足条件的元素,也就是只保留true
filter(x -> x > 5, v3) 
unique(v3) # 去重

sort

v = [3,3,6,7,2,1,9]

sort(v, rev=true# R: sort(v,decreasing = T)
sortperm(v) # R: order(v,decreasing = T)
reverse(v)

repeat

repeat(1:2, inner=3# 1 1 1 2 2 2
repeat(1:2, outer=3# 1 2 1 2 1 2 
repeat(1:23# default outer

repeat(1:2, inner=[3,5]) # matrix
repeat(1:2, outer=[3,5]) # matrix
repeat(1:2,3,5# 3行5列

Math

maximum(v) # 求最大值 R:max(v)
minimum(v) 
argmax(v) # 求最大值首次出现的下标
argmin(v) 
findmax(v) # 返回最值和相应的首次出现的下标
findmin(v)  

v=[1,2,3]

sum(v)
prod(v) #求乘积
# 对于布尔型数组
all([true,false,true]) # 判断所有元素为真
any([true,false,true]) # 判断存在真值元素
[1,2,"A"] == [1,2,'A'# 两个等长数组的对应元素是否完全相同

Matrix

Create

m = [1 2 34 5 6]
size(m) # nrow, ncol
size(m,1# nrow
size(m,2# ncol

m = reshape(1:623)

初始化

zeros(2,3)
ones(Int6423)
ones(String23)

Array{Float64}(undef, 23)

similar(m)* # 生成元素类型和大小都与m相同, 但是元素未初始化的数组*

合并

m1 = [1 2 34 5 6]
m2 = [7 8 91 2 3]

hcat(m1,m2) # R: cbind(m1,m2)
vcat(m1,m2) # R: rbind(m1,m2)

Retrieve

A1[2,3]
A1[2,3] = -6  # mutate

A1[:, 2# vectror
A1[2, :] # vectror

A1[1:22:3# matrix

A1[1:22:3] = [102 103202 203# mutate

对每行或每列的操作

m1 = [1 2 34 5 6]

mapslices(sum, m1, dims=1# R: apply(m1,1,sum)
[sum(x) for x in eachrow(m1)]

mapslices(sum, m1, dims=2# R: apply(m1,2,sum)
[sum(x) for x in eachcol(m1)]

转置

# R: t(m1)
# Python: m1.T; m1.transpose()
m1' == transpose(m1) 

tuple

(1"John"5.1# ,标识 
tn1 = (name="John", age=32# named tuple
tn1[:name] # Retrieve
tn1.name # Retrieve

Pair

x = "apple" => 1
first(x)=="apple"
last(x)==1

dictionary

Create

d = Dict("name" => "Li Ming""age" => 18)

d2orig = [('a'1), ('b'2), ('c'3), ('d'4)]
d2 = Dict(d2orig)

x = ['a''b''c''d'
y = [1,2,3,4]
d2 = Dict(zip(x, y)) #x,y是等长的数组

# ----------------------指定类型---------------------
Dict{StringInt64}("apple" => 1"pear" => 2"orange" => 3#指定类型

# ----------------------字典生成器----------------------
Dict(x => x*x for x in [2,3,5,7])

Retrieve

d["age"]
get(d,"age",0# 不存在age返回0
get!(d, "age",0)#不存在age,则add 
 
# ----------------------键值是否存在----------------------
haskey(d, "gender"
"gender" in keys(d)

d["gender"] = "Male" # add

delete!(d, "gender"#delete "gender"

遍历

# ----------------------遍历key----------------------
for k in keys(d2)
    println(k, " => ", d2[k])
end
# ----------------------遍历value----------------------
values(d2)
collect(values(d2)) # vector
# ----------------------二元遍历----------------------
for (k,v) in d2
    println(k, " => ", v)
end

Set

Create

Set(1:3)

Set(['a''b''c''b']) #去重

Set("keep"# 'k''e''p' # Set{Char} with 3 elements
Set(["keep"]) # "keep" # Set{String} with 1 element  

methods

union(A, B) #或A ∪ B:并集。∪输入为\cup
intersect(A, B) # A ∩ B: 交集。∩输入为\cap<TAB>。
setdiff(A, B) #或A \ B:差集。
issetequal(A, B) # 集合相等
issubset(A, B) # 或A ⊆ B #子集,⊆输入为\subseteq<TAB>。
#属于关系用in, ∈(\in+TAB),∉(\notin+TAB)
push!(x, a) # 元素a加入到集合x中

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多