Home > Back-end >  How do I store the whole iteration result?
How do I store the whole iteration result?

Time:09-22

I'm having a problem in storing the whole results after iteration(for-loop). I would like to save prob0 for each i in result_negative.append((' '.join(i), prob0)). However, only last prob0 is saved from the first to last row. Does anyone know where I made a mistake?

result_negative = []
for i in result_te:
    prob0 = math.log(c0)
    for word in i:
        if word in set0[0]:
            prob0 = math.log(set0[set0[0] == word]['prob'])   prob0
        else :
            prob0 = math.log(1) prob0
    
    result_negative.append((' '.join(i), prob0))

Edited) I'm sorry. The values I got are not the results of iteration. it was the initial value, prob0 = math.log(c0). Now I'm pretty confused

CodePudding user response:

Here you go:

result_negative = []
for i in result_te:
    prob0 = math.log(c0)
    for word in i:
        if word in set0[0]:
            prob0 = math.log(set0[set0[0] == word]['prob'])   prob0
        else :
            prob0 = math.log(1) prob0
            result_negative.append((' '.join(i), prob0)) #now this is inside the loop 

CodePudding user response:

Like below, as @Tim Roberts and @Frank Yellin said, and @personaltest25's solution corrected:

result_negative = []
for i in result_te:
    prob0 = math.log(c0)
    for word in i:
        if word in set0[0]:
            prob0 = math.log(set0[set0[0] == word]['prob'])   prob0
        else :
            prob0 = math.log(1) prob0
        result_negative.append((' '.join(i), prob0))
  • Related