python - Appending strings to a list -


novice question here.

i'm working on finding every single combination of various string elements, , want place them list.

import itertools   mydata = [ ]  main = [["car insurance", "auto insurance"], ["insurance"], ["cheap", "budget"],       ["low cost"], ["quote", "quotes"], ["rate", "rates"], ["comparison"]]  def twofunc(one, two):     a, b in itertools.product(main[one], main[two]):         print a, b  def threefunc(one, two, three):     a, b, c in itertools.product(main[one], main[two], main[three]):         print a, b, c  twofunc(2, 0)  #extremely inefficient run these functions on , over. alternative? twofunc(3, 0) twofunc(0, 4) twofunc(0, 5) twofunc(0, 6) threefunc(2, 0, 4) threefunc(3, 0, 4) threefunc(2, 0, 5) threefunc(3, 0, 5) threefunc(2, 0, 6) threefunc(3, 0, 6) 

the above code prints out each permutation, doesn't append values list. i've tried various variations on append method , still have no luck.

can me placing these values mydata list. assume each string have seperate list, end being list of lists. should following, i'll need way include "tags" in future, or values if string contains value.

[["cheap car insurance"], ["cheap auto insurance"], ["budget car insurance"], ["budget auto insurance"], ["low cost car insurance"], ["low cost auto insurance"], ... 

so, eventually, end up: 1 means strings contains word car/cheap, , 0 means doesn't. reason mention inquire if list proper data structure task.

                         car        cheap  cheap car insurance       1            1 cheap auto insurance      0            1  budget car insurance      1            0 budget auto insurance     0            0 

can help.

i've completed task in r, pretty amenable task, , wanted reproduce in python.

to return values of twofunc , threefunc list, can change return statements lists returned. append result mydata. example follows twofunc:

def twofunc(one, two):     a, b in itertools.product(main[one], main[two]):         return [a, b]  mydata.append(twofunc(2,0)) 

that said, i'm not familiar r, don't know data structure might using there. stated goal, keeping in list might complicated. however, creating simple class shouldn't difficult. have list made of instantiations of class.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -