Welche Zahl wird ausgekocht? [Schneller] als Python (http://julialang.org/benchmarks/) ähnelt das Schreiben Python und ist heutzutage die auffälligste Sprache in mir.
Dieses Mal werde ich die Unterschiede in der Syntax vorstellen, die Pythonista bei der Verwendung von Julia beachten muss, indem ich Python und Julia vergleiche.
Klicken Sie hier, um Julias Umgebung zu erstellen (http://julialang.org/downloads/).
Der Code wird aus Gründen der Übersichtlichkeit eingerückt, der Einzug bezieht sich jedoch nicht auf das Verhalten.
julia
for n = 1:10
println(n)
end
Wenn Sie in einer Zeile schreiben, benötigen Sie ;
.
julia
for n = 1:10;println(n);end
Außerdem ist der Index der Liste von ** 1 **, nicht von 0.
julia
julia> List = [0, 1, 2]
3-element Array{Int64,1}:
0
1
2
julia> println(List[1])
0
Hello World!
python
print "Hello World!"
julia
println("Hello World!")
for Loop statements
python
for i in range(10):
print i
julia
for n = 0:9
println(n)
end
If statements
python
hoge = "hoge"
if hoge[0] == 'h' and hoge[1:] == "oge":
print "hoge is hoge"
elif hoge == "huge" or hoge == "hige":
print "hoge is huge or hige"
else:
print "hoge is", hoge
In Juliaand
Ist&&
、or
Ist||
ist.
julia
hoge = "hoge"
if hoge[1] == 'h' && hoge[2:4] == "oge"
println("hoge is hoge")
elseif hoge == "huge" || hoge == "hige"
println("hoge is huge or hige")
else
println("hoge is ", hoge)
end
Es funktioniert mit hoge [2:]
, aber * WARNUNG: veraltete Syntax "x [i:]". * Wird angezeigt, also habe ich hoge [2: 4]
geschrieben.
Exception Handling (Try-Except)
python
List = [0, 1, 2]
try:
print List[3]
except IndexError as e:
print e
julia
List = [0, 1, 2]
try
println(List[4])
catch e
if isa(e, BoundsError) # "BoundsError"Sagt in Python"IndexError"ist
println(e)
end
end
Functions
python
def huge(x, y):
z = x + y
return z
julia
function huge(x, y)
x + y
end
Oder
julia
function huge(x, y)
z = x + y
return z
end
Anonymous Functions
python
z = lambda x, y : x + y
z(1, 2)
julia
z = (x, y) -> x + y
z(1, 2)
Working with Files
Read
python
with open("output.txt") as f:
for line in f:
print line.rstrip() #Der Zeilenvorschubcode wurde entfernt, um mit der Julia-Ausgabe übereinzustimmen
julia
open("output.txt") do f
lines = readlines(f)
for line in lines
print(line) #Julia drucken()Enthält keinen Zeilenvorschubcode
end
end
Oder
julia
open("output.txt") do f
for line in eachline(f)
print(line)
end
end
Write
python
with open("output.txt", "w") as f:
for i in range(10):
f.write(str(i) + "\n")
julia
open("output.txt", "w") do f
for n = 0:9
write(f, string(n)"\n")
end
end
List complemention
python
[[n**i for n in range(10)] for i in range(5)]
julia
[[n^i for n = 0:9] for i = 0:4]
Erstellen Sie eine Liste jedes Elements plus 10
python
[n+10 for n in range(10)]
julia
10+[0:9]
printf
python
Integer = 2
String = "hoge"
print("Integer: %d String: %s" % (Integer, String))
julia
Integer = 2
String = "hoge"
@printf "Integer: %d String: %s" Integer String
strip()
python
>>> "hoge\n".rstrip()
'hoge'
julia
julia> rstrip("hoge\n", '\n')
"hoge"
infix form
julia
julia> +(10, 20, 30)
60
julia> *(10, 20, 30)
6000
Oder
julia
julia> f = +
+ (generic function with 117 methods)
julia> f(10, 20, 30)
60
julia> f = *
* (generic function with 115 methods)
julia> f(10, 20, 30)
6000
inverse divide
julia
julia> 1/3
0.3333333333333333
julia> 3\1
0.3333333333333333
Function chaining
julia
julia> [0:5] |> x -> x.^x |> sum
3414
Recommended Posts