D is a list containing some words. D=[word1,word2,word3......]
E is a list containing combinations of any two words in D ... E=[(word1,word2),(word7,word1)....]
How can I implement the following in python:

A0[x]=1 for all x in D           # This is actually Ai. when i=0, it becomes A0
B0[x]=1 for all x in D
for i = 1 to 10:                 # i takes the values 1 to 10
    Ai[x]=0 for all x in D
    Bi[x]=0 for all x in D
    for x in D:
       for y in D:
          if (x,y) in E:
               Bi[x]=bi[x] + Ai-1[q]
          if (y,x) in E:
               Ai[p]=Ai[p]+Hi-1[q]

print A10,B10

Recommended Answers

All 6 Replies

And your code? That pseudo code is not yours, is it?

no its not mine!!
I am trying to implement it... but i can only think of the worst case scenario. ie., to create dictionaries from A0 to A9 and from B0 to B9. And then have loops like when i ==1 do this, i ==2 do that....etc... which is very bad...

no its not mine!!
I am trying to implement it... but i can only think of the worst case scenario. ie., to create dictionaries from A0 to A9 and from B0 to B9. And then have loops like when i ==1 do this, i ==2 do that....etc... which is very bad...

A0={},A1={},.....A9={}
B0={},B1={},.....B9={}


for x in D:
    A0[x]=1      
    B0[x]=1
    for i = 1 to 10:               
        if i ==1:
            A1[x]=0 
            B1[x]=0
        .
        .
        .
        elif i == 9:
            A9[x]=0
            B9[x]=0
        
        for m in D:
            for n in D:
               if (m,n) in E:
                   if i==1:
                       B1[m]=B1[m] + A0[n]
                   .
                   .
                   .
                    if i==9:
                        B9[m]=B9[m] + A8[n]
               elif (n,m) in E:
                    if i==1:
                        A1[m]=A1[m]+B0[n]
                    .
                    .
                    .
                    if i==9:
                         A9[m]=A1[m]+B8[n]
 
print A9,B9

What's the expected output ?

(m,n) means m refers n
The expected output is:
how many times m refers to all n which should be put in B
& how many times n is referred which should be put in A

(m,n) means m refers n
The expected output is:
how many times m refers to all n which should be put in B
& how many times n is referred which should be put in A

It sounds like a job for itertools.groupby (which uses sorted data). Try something like

def scoreB((m,n)): return m
def scoreA((m,n)): return n

gA = itertools.groupby(sorted(E, key=scoreA), key=scoreA)
gB = itertools.groupby(sorted(E, key=scoreB), key=scoreB)

Then iterate over gA and gB to examine their content.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.