Bottom Page

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!
Quote
#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']
pianistseb likes this post
Quote
#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!
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding MINIMUM number in a random list is not working Mona 5 155 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Why does pop() removes an element from more than one list? albufork 5 131 Nov-17-2019, 08:01 AM
Last Post: buran
  can't remove element from a list yokaso 6 164 Nov-06-2019, 03:11 PM
Last Post: yokaso
  get the one element from 1-list or 1-set Skaperen 1 85 Nov-05-2019, 11:50 PM
Last Post: Gribouillis
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 211 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Replace string in many files in a folder metro17 8 357 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 189 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Replace Items in List. mcmxl22 3 203 Oct-07-2019, 05:05 AM
Last Post: Larz60+
  open, read and replace a string in a file Reims 0 150 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 283 Sep-15-2019, 01:24 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)