【问题标题】:How can i do this javascript code on Python 3?我如何在 Python 3 上执行此 javascript 代码?
【发布时间】:2021-02-14 14:06:33
【问题描述】:

我在 JavaScript 中有这行,但我想知道 Python 中的等价物。

const { entities, classification } = obj

完整的功能是:

function extractActionEntities (lang, expressionsFilePath, obj) {
    return new Promise(async (resolve, reject) => {
        log.title('NER')
        log.info('Searching for entities...')

        // Need to instanciate on the fly to flush entities
        this.nerManager = new NerManager()

        const { entities, classification } = obj
        // Remove end-punctuation and add an end-whitespace
        const query = `${string.removeEndPunctuation(obj.query)} `
        const expressionsObj = JSON.parse(fs.readFileSync(expressionsFilePath, 'utf8'))
        const { module, action } = classification
        const promises = []
    });
}

【问题讨论】:

  • Stack Overflow 不是代码编写服务,在提出问题之前,您至少必须诚实地尝试解决您的问题。
  • 也许你不明白,或者我没有让自己明白。我不想让他们为我写代码,我只想知道我是如何在 Python 中做到这一点的。
  • 我的错,我没有注意 line 部分。如果您想编辑问题中的任何内容,我可以删除 downvote 投。
  • 抱歉,我无法删除该行,我不想添加该行,但 Stack Overflow 的政策迫使我这样做。没关系,我得到了答案。

标签: javascript python


【解决方案1】:

Python 不使用常量,因此您只需移植 Python 也没有的解构(至少不完全像 ES6)。

但是,您可以这样做:

entities, classification = [obj[k] for k in ("entities", "classification")]

或者,根据this,使用itemgetter

from operator import itemgetter
entities, classification = itemgetter("entities", "classification")(obj)

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2021-01-27
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多