471,349 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

how to insert a string into a string of random letters in a for loop

111 100+
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code
Expand|Select|Wrap|Line Numbers
  1. def random_seq():
  2.     seq=""
  3.     ch=""
  4.     for i in range(0,50):
  5.         ch=random.choice(("ATGC"))
  6.         seq=seq+ch
  7.         for i in seq:
  8.  
  9.             seq[i:i+15]="CGTCAAGTTCAAGTGC"
  10.     return seq
  11.  
  12.  
now here in this I am creating a list of sequence containing the letters only "ATGC".Now I want to specify in this that say at particular postions like say the first fifteen characters should be what i mentioned. and say i also want to do when for positions from 41 to 50 i want a specifies set of letters something like this " "CGTCAAGTTCAAGTGC".how to do this?
why is my code not working?
waiting for your reply
cheers!
Jul 12 '07 #1
2 2819
bartonc
6,596 Expert 4TB
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code
Expand|Select|Wrap|Line Numbers
  1. def random_seq():
  2.     seq=""
  3.     ch=""
  4.     for i in range(0,50):
  5.         ch=random.choice(("ATGC"))
  6.         seq=seq+ch
  7.         for i in seq:
  8.  
  9.             seq[i:i+15]="CGTCAAGTTCAAGTGC"
  10.     return seq
  11.  
  12.  
now here in this I am creating a list of sequence containing the letters only "ATGC".Now I want to specify in this that say at particular postions like say the first fifteen characters should be what i mentioned. and say i also want to do when for positions from 41 to 50 i want a specifies set of letters something like this " "CGTCAAGTTCAAGTGC".how to do this?
why is my code not working?
waiting for your reply
cheers!
I hope this is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def random_seq(nChars, insertAt, aString):
  4.     seq = ""
  5.     for i in range(nChars):
  6.         if i == insertAt:
  7.             seq += aString
  8.         ch=random.choice(("ATGC"))
  9.         seq += ch
  10.     return seq
  11.  
  12. # I put markers in your string so you can see and make it an argurment to the function
  13. theStringToInsert = "#CGTCAAGTTCAAGTGC#"
  14. # calculate the count as total - the length of the string to insert
  15. count = 50 - len(theStringToInsert)
  16.  
  17.  
  18. result = random_seq(count, 15, theStringToInsert)
  19. print result, "is %d characters long" %len(result)
  20. print result.find('#')
  21.  
TATGTAGATACAGCT#CGTCAAGTTCAAGTGC#CGGGGTAGCCCGTTCAT is 50 characters long
15
Jul 12 '07 #2
aboxylica
111 100+
Thanks a lot!that helped.But I still have that matrix problem.
waiting for ur reply,
cheers!
I hope this is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def random_seq(nChars, insertAt, aString):
  4.     seq = ""
  5.     for i in range(nChars):
  6.         if i == insertAt:
  7.             seq += aString
  8.         ch=random.choice(("ATGC"))
  9.         seq += ch
  10.     return seq
  11.  
  12. # I put markers in your string so you can see and make it an argurment to the function
  13. theStringToInsert = "#CGTCAAGTTCAAGTGC#"
  14. # calculate the count as total - the length of the string to insert
  15. count = 50 - len(theStringToInsert)
  16.  
  17.  
  18. result = random_seq(count, 15, theStringToInsert)
  19. print result, "is %d characters long" %len(result)
  20. print result.find('#')
  21.  
TATGTAGATACAGCT#CGTCAAGTTCAAGTGC#CGGGGTAGCCCGTTCAT is 50 characters long
15
Jul 12 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Bart Nessux | last post: by
21 posts views Thread by Andreas Lobinger | last post: by
2 posts views Thread by Joe | last post: by
11 posts views Thread by Olaf \El Blanco\ | last post: by
4 posts views Thread by darrel | last post: by
7 posts views Thread by Johny | last post: by
3 posts views Thread by miraan | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.