python - 什么是 julia 中的 ".=="以及它在 python 中的等价物?

标签 python python-3.x julia

我是 julia 的新手,我正在努力将 julia 代码重写为 python 代码。
我看到了一些使用 .== 的代码表达。我无法理解这是什么意思。所以我在网上搜索它,但找不到答案。
谁能告诉我什么是.==在 julia 和 python 中的等价物?
仅供引用,它是这样写的。

x = sum(y .== 0)  # y is array

最佳答案

那是一个 Vectorized dot operation and 用于将运算符应用于数组。您可以通过列表推导对 python 中的一维列表执行此操作,但在这里您似乎只是在计算所有零,所以

>>> y = [0,1,1,1,0]
>>> sum(not bool(v) for v in y)
2
其他软件包如 numpypandas将向量化运算符,所以这样的事情会做
>>> import numpy as np
>>> y = np.array([0,1,1,1,0])
>>> (y == 0).sum()
2
>>>
>>> import pandas as pd
>>> df=pd.DataFrame([[0,1,2,3], [1,2,3,0], [2,3,4,0]])
>>> (df==0).sum()
0    1
1    0
2    0
3    2
dtype: int64
>>> (df==0).sum().sum()
3

关于python - 什么是 julia 中的 ".=="以及它在 python 中的等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65418722/

相关文章:

mysql - 连接到 MySQL 数据库并在 Julia 中获取数据

python - 内存上限?

python - Plotly:如何控制双 y 轴的哪条迹线在前面?

json - 如何将 JSON 对象写入文件以便稍后在 JuliaLang 中读取?

python - 为什么 -2//4 的输出为 -1?

python-3.x - 将 Numpy 结构数组保存到 *.mat 文件

multidimensional-array - Julia - 使用 mvNormal 生成具有给定均值和协方差矩阵的多元高斯样本

python - TypeError : array( ['cycling' ], dtype=object) 不可 JSON 序列化

python - 尝试制作一个根据用户输入用 "?"替换辅音单词的 Python 程序

python - 正则表达式对非捕获组的帮助