在python中,“args=[temp[n]for n In array(index)]”是否检查temp[n]?

2024-05-13 02:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从Python转换成Java。你知道吗

我的问题是“args”在做什么?你知道吗

args = [this.scrath[c] for c in this.connections(n)]; //Python

是不是:

[this.scrath[c] //get data at index c of this.scratch[]

for c in // for number of c in connections

this.connections(n)]; //connections to ANN_Neuron n

在这种情况下这个。刮痕[c] “检查数据是否与中的c匹配”这个。连接(n) “?你知道吗

this.scratch = Arrays.copyOfRange(inputValues, this.scratch.length-this.input_length, this.scratch.length+1); //JAVA

//inputValues given as negative values.
for (int i=0; i<this.scratch.length; i++){
    this.scratch[i] = inputValues[i]*-1;
}

//loop through the active genes in order
for (ANN_Neuron n : nodes){
    if (n.active){
        float func = n.function;
        for (ANN_Connection c : n.connections){
        //Argument here!!
        }
    }

    args = [this.scrath[c] for c in this.connections(n)]; //Python

    //apply function to the inputs from scratch, save results in scratch
    this.scratch[n] = function(*args);
}

Tags: ofthetoinforargsfunctionthis
3条回答

这是:

args = [this.scrath[c] for c in this.connections(n)]

相当于:

args = []
for c in this.connections(n):
    args.append(this.scrath[c])

[a for b in c]是一个列表理解。它通过遍历列表(或其他iterable)中的每个元素c,调用该元素b,然后计算表达式a,并将结果放入结果列表中,从而生成一个列表。你知道吗

我觉得你在向后看。你知道吗

# Python:
args = [f(x) for x in iter]

类似于

// Java:
List<Type> args = new ArrayList<Type>(iter.size());
for (Type x : iter)
    args.add(f(x));

所以在[f(x) for x in iter]中,将x分配给iter的每个元素,对f(x)进行求值,并将结果收集到一个列表中。你知道吗

相关问题 更多 >