Question

I have two sets

tf_ar=[0.0,0.032,0.235,0.65,0,....]  and
idf=[1.2,1.6,0.68,....]

I have to do multiplication of idf and tf_ar so that each term in idf multiply to six terms in tf_ar. It implies that

(number of terms in tf_ar)= [6*(number of terms in idf)]

How should i need to give conditions for it so it will continue to loop for next six terms in tf_ar?

j=0
for term in idf:
    i=0
    for tf in tf_ar:
        if i==6:
            break
        else:
            tf_idf+=str(float(tf)* idf[j])+','

    i+=1
tf_idf=tf_idf.strip()+'\n'
j+=1

For Example:

tf_ar=[1,2,3,4,5,6,7,8,9,10,11,12...]
idf=[A,B,...]

I want result as

tf_idf=[1A,2A,3A,4A,5A,6A,7B,8B,9B,10B,11B,12B...]

I want idf should be multiplied by six terms of tf_ar and should continue with next terms.

Was it helpful?

Solution 2

This might give you some ideas, though I'm sure it's not 100% correct for your specific use case because the code you posted is sort of unclear. But you should be able to adapt this to your needs.

result = []
for ii, term in enumerate(idf):
    result.append(0)
        for tf in tf_ar[6*ii:6*(ii+1)]:
            result[-1] += term * tf

The key idea is to "enumerate" to get an integer index to go along with the iteration over idf, then make "slices" with [x:y] of tf_ar which are 6 wide each.

OTHER TIPS

it seems like something like this would do the trick for you:

[a * b for a in idf for b in tf_ar]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top