给出2个嵌套列表,并计算最里面的一个元素

时间:2018-08-19 16:02:58

标签: python

如果我有2个列表,例如:

主题和功劳:

[['Math',6],['English',3],['Drama',2]]

学生和成绩:

[['Amy',,90,80],['Jack','60','30','50'],['Tom','80','','50']]

,并希望像这样计算它们:

对于艾米,数学没有分数。英语90分(3学分),戏剧80分(2学分) 因此它将输出

[['Amy',,270,160],['Jack',360,90,100],['Tom'480,,100]]

编辑:

我自己解决了。我只需要使用类似的东西

a[i][j] = b[i][j] *c [i][1]

2 个答案:

答案 0 :(得分:2)

请执行以下操作:

void YourDSP::process(AUAudioFrameCount frameCount, AUAudioFrameCount bufferOffset) {

    for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
        int frameOffset = int(frameIndex + bufferOffset);

        float temp = 0;
        for (int channel = 0; channel < _nChannels; ++channel) {
            float *out = (float *)_outBufferListPtr->mBuffers[channel].mData + frameOffset;

            *out = ... whatever your magic is ...
        }
    }
}

也就是说,您可能应该实现这种更简单的方法,而无需列表理解。

答案 1 :(得分:0)

如果要处理学生和分数,则可能要使用词典或对象而不是列表。例如:

from typing import List

class Student(object):
    def __init__(self, name: str):
        self.name = name
        self.subjects_ = []

    def __repr__(self):
        return self.name

    def add_subject(self, subject_, score_: int):
        subject = Subject(subject_, score_)
        self.subjects_.append(subject)
        print(f"added {subject_} to {self.name} with a score of {score_}")

    def remove_subject(self, subject_):
        self.subjects_.remove(subject_)

    def subjects(self):
        return [s.name for s in self.subjects_]`

class Subject(object):
    def __init__(self, name: str, credits: int):
        self.name = name
        self.credits = credits
    def __repr__(self):
        return self.name

students = ['Amy', 'Jack', 'Tom']
math = Subject('math', 4)
history = Subject('history', 8)

amy = Student(students[0])
amy.add_subject(math,10)
amy.add_subject(history,6)

print(amy.subjects())