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..
#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


Messages In This Thread
RE: Replace a list or string element with a random one.. - by buran - May-09-2019, 08:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 408 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 465 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 721 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 778 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,258 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Replace string in a nested Dictianory. SpongeB0B 2 1,220 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,608 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  List of random numbers astral_travel 17 2,725 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,239 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,858 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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