Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

paynito

  • 2 years ago

help with optional reading think python exercise 8.2. How do I slice and insert new dna into a string and splice it back together again? code attached: #http://www.greenteapress.com/thinkpython/thinkpython.pdf #chapter 8 for loops #brandon MOOC group 3 #oct. 20 prefixes = 'JKLMNOPQ' suffix = 'ack' news=[] for letter in prefixes: news.append( letter + suffix) print news for each word in news if [:1] == 'O': #finds Oack in the list #code to mutate Oack to Ouack elif [:1] == "Q": #finds Qack in the list #code to insert u before ack

  • This Question is Closed
  1. FabienToune
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Like this ? prefixes = 'JKLMNOPQ' suffix = 'ack' news=[] for letter in prefixes: if letter == 'O' or letter == 'Q': letter += 'u' news.append( letter + suffix) print news

  2. FabienToune
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Btw, if you really want to "insert" a char into a String, as strings are immutable, you should probably do like this : word = word[0] + 'u' + word[1:], but then you have to replace the word in the list : for index, word in enumerate(news): if word[0] in ['O', 'Q']: word = word[0] + 'u' + word[1:] news[index] = word print news

  3. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Since you've already put it into a list, I would opt to use the list splice method... for i in range(0, len(news)): letter=news[i][:1] if letter=='O'or letter=='Q': news[i]=news[i][:1]+'u'+news[i][1:] As @FabienToune put it, you will need to replace the word after changing it.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.