ok now i was wondering if am gonna use itertools.product but i want it to start from a specific string i mean if we had itertools.product("abcd") it will give you "a","b","c","d"... so what if i wanted to start from "b"??
first method :
assign the first string to a variable and use if statement to keep checking if it is equals example:
>>import itertools
>>m="ab"
>>it=itertools.product("abcdefg",repeat=2)
>>while true :
>>.....if "".join(it.next())==m:
>>..........break
>>for i in it:
>>....print "".join(i)
"ac"
"ad"
"ae"
"af"........
but this could take a long time :\ so what if we made our OWN iter generator just like product function???
second method:
here is the code
>>def iter(x,y=1,last=None):
>>.....if last != None:#if there is last object
>>..........result=[[]]
>>..........for pool in range(y) :
>>.............result=["".join(a)+b for a in result for b in x]
>>..........indexoflast=result.index(last)#we take the index of last
>>..........indexoflast=indexoflast+1#add one to it
>>..........result[:indexoflastresult]=[]#delete previous values
>>..........lenofresult=(len(result)-1)#length of the result minus one
>>..........for i in range(lenofresult):#the generator
>>...............yield result[i]
>>.....else:#else if there is no last object specified
>>..........result=[[]]
>>..........for pool in range(y):
>>...............result=["".join(a)+b for a in result for b in x]
>>..........for i in result:
>>...............yield i
so if we typed
>>iter("asd")
"a"
"s"
"d"
>>iter("asd",2)
"aa"
"as"
"ad"
"sa"
"ss"
"sd"
"da"
"ds"
"dd"
>>iter("asd",2,"ad")
"sa"
"ss"
"sd"
"da"
"ds"
"dd"
first method :
assign the first string to a variable and use if statement to keep checking if it is equals example:
>>import itertools
>>m="ab"
>>it=itertools.product("abcdefg",repeat=2)
>>while true :
>>.....if "".join(it.next())==m:
>>..........break
>>for i in it:
>>....print "".join(i)
"ac"
"ad"
"ae"
"af"........
but this could take a long time :\ so what if we made our OWN iter generator just like product function???
second method:
here is the code
>>def iter(x,y=1,last=None):
>>.....if last != None:#if there is last object
>>..........result=[[]]
>>..........for pool in range(y) :
>>.............result=["".join(a)+b for a in result for b in x]
>>..........indexoflast=result.index(last)#we take the index of last
>>..........indexoflast=indexoflast+1#add one to it
>>..........result[:indexoflastresult]=[]#delete previous values
>>..........lenofresult=(len(result)-1)#length of the result minus one
>>..........for i in range(lenofresult):#the generator
>>...............yield result[i]
>>.....else:#else if there is no last object specified
>>..........result=[[]]
>>..........for pool in range(y):
>>...............result=["".join(a)+b for a in result for b in x]
>>..........for i in result:
>>...............yield i
so if we typed
>>iter("asd")
"a"
"s"
"d"
>>iter("asd",2)
"aa"
"as"
"ad"
"sa"
"ss"
"sd"
"da"
"ds"
"dd"
>>iter("asd",2,"ad")
"sa"
"ss"
"sd"
"da"
"ds"
"dd"
0 comments:
Post a Comment
gotta some thing to say ?
Note: Only a member of this blog may post a comment.