Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和Julia以不同的方式计算函数?_Python_Python 3.x_Julia - Fatal编程技术网

Python和Julia以不同的方式计算函数?

Python和Julia以不同的方式计算函数?,python,python-3.x,julia,Python,Python 3.x,Julia,Python3的计算 N=123456789 sum(map(int,str(N)) ---> 45 N = 123456789 sum([Int(ch) for ch in "$N"]) ---> 477 N = 123456789 sum(map(Int, collect("$N"))) ---> 477 Julia 0.6.2计算 N=123456789 sum(map(int,str(N)) ---> 45

Python3的计算

N=123456789
sum(map(int,str(N))            ---> 45
N = 123456789
sum([Int(ch) for ch in "$N"])  ---> 477

N = 123456789
sum(map(Int, collect("$N")))  --->  477
Julia 0.6.2计算

N=123456789
sum(map(int,str(N))            ---> 45
N = 123456789
sum([Int(ch) for ch in "$N"])  ---> 477

N = 123456789
sum(map(Int, collect("$N")))  --->  477

这是为什么?

因为与Julia语句等价的python是

N=123456789
print( sum(map(ord,str(N))))
输出:

477
它将ascii ord值“1”+“2”+…“9”相加,而不是将字符串的每个字符转换为
int
,然后将
int
相加

这是不一样的:

N=123456789
sum(map(int,str(N))
它将长整数转换为字符串,将每个字符输入
int()
,然后将
'1'
转换回
1
(而不是
ord('1')
),然后将数字添加到45

,而
和(数字(N))
将是Python代码的等价物。