元组 #
一、元组基础 #
1.1 创建元组 #
使用圆括号创建元组:
julia
t1 = (1, 2, 3)
t2 = ("a", "b", "c")
t3 = (1, "hello", 3.14)
t4 = ()
t5 = (1,)
1.2 元组类型 #
julia
typeof((1, 2, 3))
typeof((1, "hello", 3.14))
typeof(())
typeof((1,))
1.3 元组是不可变的 #
julia
t = (1, 2, 3)
t[1] = 10
1.4 元组 vs 数组 #
| 特性 | 元组 | 数组 |
|---|---|---|
| 可变性 | 不可变 | 可变 |
| 类型 | 参数化类型 | 元素类型 |
| 性能 | 更快 | 稍慢 |
| 用途 | 固定大小、多返回值 | 动态大小 |
二、元组访问 #
2.1 索引访问 #
julia
t = (1, 2, 3, 4, 5)
t[1]
t[end]
t[begin]
t[2:4]
2.2 迭代 #
julia
t = (1, 2, 3)
for x in t
println(x)
end
2.3 元组信息 #
julia
t = (1, 2, 3)
length(t)
size(t)
ndims(t)
eltype(t)
三、元组操作 #
3.1 连接 #
julia
t1 = (1, 2, 3)
t2 = (4, 5, 6)
(t1..., t2...)
3.2 比较 #
julia
(1, 2, 3) == (1, 2, 3)
(1, 2, 3) === (1, 2, 3)
(1, 2, 3) < (1, 2, 4)
3.3 转换 #
julia
t = (1, 2, 3)
collect(t)
[t...]
Tuple([1, 2, 3])
四、元组解构 #
4.1 基本解构 #
julia
t = (1, 2, 3)
a, b, c = t
a
b
c
4.2 函数返回多值 #
julia
function minmax(arr)
return minimum(arr), maximum(arr)
end
min_val, max_val = minmax([1, 2, 3, 4, 5])
4.3 忽略值 #
julia
t = (1, 2, 3, 4, 5)
a, _, c = t
a
c
4.4 滑动解构 #
julia
t = (1, 2, 3, 4, 5)
a, b... = t
a
b
五、命名元组 #
5.1 创建命名元组 #
julia
nt1 = (a=1, b=2, c=3)
nt2 = (; a=1, b=2, c=3)
nt3 = (; :a => 1, :b => 2, :c => 3)
5.2 访问元素 #
julia
nt = (name="Julia", version=1.10, year=2024)
nt.name
nt.version
nt[:name]
nt[1]
5.3 命名元组类型 #
julia
typeof((a=1, b=2))
typeof((a=1, b="hello"))
5.4 键和值 #
julia
nt = (a=1, b=2, c=3)
keys(nt)
values(nt)
pairs(nt)
propertynames(nt)
5.5 合并命名元组 #
julia
nt1 = (a=1, b=2)
nt2 = (c=3, d=4)
merge(nt1, nt2)
merge(nt1, (b=20, c=30))
5.6 从变量创建 #
julia
a = 1
b = 2
c = 3
nt = (; a, b, c)
六、元组与函数 #
6.1 函数参数 #
julia
function foo(a, b, c)
return a + b + c
end
args = (1, 2, 3)
foo(args...)
6.2 关键字参数 #
julia
function bar(; a, b, c)
return a + b + c
end
kwargs = (a=1, b=2, c=3)
bar(; kwargs...)
6.3 多返回值 #
julia
function divide_and_remainder(a, b)
return (div(a, b), a % b)
end
quotient, remainder = divide_and_remainder(10, 3)
七、元组与类型 #
7.1 元组类型 #
julia
Tuple{Int, String, Float64}
Tuple{Int, Vararg{Int}}
NTuple{3, Int}
7.2 类型参数 #
julia
function process(t::Tuple{Int, String})
println("Int: $(t[1]), String: $(t[2])")
end
process((1, "hello"))
process((1, 2))
7.3 Vararg #
julia
function varargs(args...)
println("Got $(length(args)) arguments")
for (i, arg) in enumerate(args)
println("$i: $arg")
end
end
varargs(1, 2, 3, "hello")
八、实践练习 #
8.1 练习1:坐标点 #
julia
function distance(p1::Tuple{Float64, Float64}, p2::Tuple{Float64, Float64})
sqrt((p2[1] - p1[1])^2 + (p2[2] - p1[2])^2)
end
distance((0.0, 0.0), (3.0, 4.0))
8.2 练习2:命名元组配置 #
julia
function create_config(; host="localhost", port=8080, debug=false)
(; host, port, debug)
end
config = create_config(port=3000, debug=true)
config.host
config.port
8.3 练习3:多返回值 #
julia
function analyze_string(s::String)
(
length = length(s),
words = length(split(s)),
uppercase = count(isuppercase, s),
lowercase = count(islowercase, s)
)
end
result = analyze_string("Hello World Julia")
result.length
result.words
8.4 练习4:元组解构 #
julia
function swap(a, b)
(b, a)
end
x, y = swap(1, 2)
x
y
九、总结 #
本章我们学习了:
- 元组创建:圆括号语法
- 元组访问:索引和迭代
- 元组操作:连接、比较、转换
- 元组解构:多变量赋值
- 命名元组:键值对访问
- 元组与函数:参数展开和多返回值
接下来让我们学习Julia的字典!
最后更新:2026-03-27