Python 这段代码做了什么?列表=列表[列表!=值]

标签 python

如标题所示,ans_list是一个答案列表,ans_index是一个数字(答案在词汇表中的索引,但与atm无关)

这里生成的 tree.anslist 是什么? (例如,仅针对第一个),忽略迭代。

for tree in train_trees:
    tree.ans_list = ans_list[ans_list != tree.ans_ind]

我自己尝试了一些代码,但没有成功得出任何意义

更新:我错过了一行将数字列表更改为数组的代码。

ans_list = array([vocab.index(ans) for ans in ans_list])

最佳答案

ans_list != tree.ans_ind 是一个 bool 值。 True 和 False 在底层表示为整数 0 和 1(这是一个古老的传统,可以追溯到非强类型语言,例如 c 语言就是这样做的)。因此它根据 bool 语句是真还是假返回列表中的第一项或第二项。

>>> l = ['a', 'b', 'c']
>>> l[False]
'a'
>>> l[True]
'b'

关于Python 这段代码做了什么?列表=列表[列表!=值],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204092/

相关文章:

python - 重置 pandas 中多级列的索引,以便较高的索引 perfaces 较低的索引

python - 我可以指望在 Python 元组中保留顺序吗?

python - python如何从不在sys.path中的路径加载模块

python - 元组列表(从元组中删除特殊项目)

python - 如何在图表中运行 elif 函数?

python - Pytorch和多项式线性回归问题

python - 使用 Django REST 框架从多个模型返回结果

python - 使用 HAAR 级联和 OpenCV 识别图像中的对象

python - attributeError ("' _AssertRaisesContext' 对象没有属性 'exception' ",),

python - 线程启动时出现未处理的异常 python manage.py runserver