Python Forum
Replace a list or string element with a random one..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace a list or string element with a random one..
#1
Hello!

I have to do a very simple task, but I have problems with speed. I have a list called 'chromosome', and I want to replace each N into A,G,C or T at random. I made this code, which is very slow (chromosome in this code is a list):

for i in range(0,len(chromosome)):
    if (chromosome[i] is not ('A' or 'C' or 'G' or 'T')):
        r=rd.randint(1,4)
        if r==1:
            chromosome[i]='A'
        elif r==2:
            chromosome[i]='G'
        elif r==3:
            chromosome[i]='C'
        else:
            chromosome[i]='T'
Can I do something to make it run faster?

Thank you!
Reply
#2
First of all let's fix a bug in your code. You will be surprised to learn that
    if (chromosome[i] is not ('A' or 'C' or 'G' or 'T')):
doesn't do what you expect. see for yourself:

chromosomes = 'NACGT'
for chromosome in chromosomes:
    print(chromosome, chromosome is not ('A' or 'C' or 'G' or 'T'))
Output:
N True A False C True G True T True
so you replace everything that is not A

you have two alternatives
if chromosome == 'N': # replace just N
or
if chromosome not in 'ACGT': # replace everything that is not A, C, G or T
or

if chromosome not in ['A', 'C', 'G', 'T']: # replace everything that is not A, C, G or T
something like this
import random
chromosomes = ['N', 'A', 'C', 'G', 'N', 'T']
new_chromosomes = [random.choice('ACGT') if c == 'N' else c for c in chromosomes]
print(new_chromosomes)
Output:
['A', 'A', 'C', 'G', 'C', 'T']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you very much for your solution! I didn't put 'if is equal to N' because sometimes in chromosomes you can find also other letters like R or M. Your code is really simple and efficient!
Reply
#4
(May-09-2019, 08:21 AM)pianistseb Wrote: I didn't put 'if is equal to N' because sometimes in chromosomes you can find also other letters like R or M.

do you want to replace these? if you want to replace everything not in ACGT, then use any of the other two example conditions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Entry field random pull from list, each return own line Bear1981 6 703 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  removing one list element without using its index paul18fr 7 1,014 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 1,988 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
Question [SOLVED] How to replace characters in a string? Winfried 2 967 Sep-04-2024, 01:41 PM
Last Post: Winfried
  extract an element of a list into a string alexs 5 3,227 Aug-30-2024, 09:24 PM
Last Post: alexs
  element in list detection problem jacksfrustration 5 1,751 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 1,772 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 1,562 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 1,863 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  find random numbers that are = to the first 2 number of a list. Frankduc 23 6,897 Apr-05-2023, 07:36 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020