473,248 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,248 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 2901
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

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

Similar topics

6
by: Bart Nessux | last post by:
number = random.sample(range(count), 1) This makes 'number' into a one entry list (I don't know why as 'count' is an integer). Is there an easy way to convert 'number' back to an int? TIA
21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
2
by: Joe | last post by:
Hi, I want to generate a random password every time a new user account is created. I want to include small and capital letters and 0 to 9 digits. Can someone give me some idea as how should I do...
11
by: Olaf \El Blanco\ | last post by:
How can i generate random words? ('a'..'z') Is there any function that convert a number to it ascci char? My english is horrible! Here an example: function(65) return 'a'; Thank you!
4
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short...
1
by: gry | last post by:
I want a function (or callable something) that returns a random word meeting a criterion. I can do it like: def random_richer_word(word): '''find a word having a superset of the letters of...
7
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
3
by: miraan | last post by:
Hi, Right now I am working on creating a string of 5 random letters. I have already created the script, it does create a unique random letter but it repeats it 5 times. Here is what I have so far: ...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.