How can I rewrite this to give me a list of answers?












-2














I am new to python and programming, any help would be much appreciated! I am having trouble with a piece of coding I am writing in Python. My task is to write a function to calculate the number of times a particular letter appears divided by the total number of letters in that sequence.

My function is supposed to return a list with the fraction of letters for each sequence.



def calculate_let(sequences, letter):
calculate_letNew =
for seq in sequences:
len(seq)
calculate_letNew = seq.count(letter)/len(seq)
return calculate_letNew


This is my code so far. The output gives me a fraction for only one sequence even tough the list sequenceA has four sequences.



Letter_A = calculate_let(sequenceA, 'A')
print(Letter_A)


output: 0.10273972602739725



I have been trying my hardest to try and solve this problem, but quite honestly don't know where to start.










share|improve this question




















  • 4




    Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
    – khelwood
    Nov 20 at 23:03
















-2














I am new to python and programming, any help would be much appreciated! I am having trouble with a piece of coding I am writing in Python. My task is to write a function to calculate the number of times a particular letter appears divided by the total number of letters in that sequence.

My function is supposed to return a list with the fraction of letters for each sequence.



def calculate_let(sequences, letter):
calculate_letNew =
for seq in sequences:
len(seq)
calculate_letNew = seq.count(letter)/len(seq)
return calculate_letNew


This is my code so far. The output gives me a fraction for only one sequence even tough the list sequenceA has four sequences.



Letter_A = calculate_let(sequenceA, 'A')
print(Letter_A)


output: 0.10273972602739725



I have been trying my hardest to try and solve this problem, but quite honestly don't know where to start.










share|improve this question




















  • 4




    Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
    – khelwood
    Nov 20 at 23:03














-2












-2








-2


0





I am new to python and programming, any help would be much appreciated! I am having trouble with a piece of coding I am writing in Python. My task is to write a function to calculate the number of times a particular letter appears divided by the total number of letters in that sequence.

My function is supposed to return a list with the fraction of letters for each sequence.



def calculate_let(sequences, letter):
calculate_letNew =
for seq in sequences:
len(seq)
calculate_letNew = seq.count(letter)/len(seq)
return calculate_letNew


This is my code so far. The output gives me a fraction for only one sequence even tough the list sequenceA has four sequences.



Letter_A = calculate_let(sequenceA, 'A')
print(Letter_A)


output: 0.10273972602739725



I have been trying my hardest to try and solve this problem, but quite honestly don't know where to start.










share|improve this question















I am new to python and programming, any help would be much appreciated! I am having trouble with a piece of coding I am writing in Python. My task is to write a function to calculate the number of times a particular letter appears divided by the total number of letters in that sequence.

My function is supposed to return a list with the fraction of letters for each sequence.



def calculate_let(sequences, letter):
calculate_letNew =
for seq in sequences:
len(seq)
calculate_letNew = seq.count(letter)/len(seq)
return calculate_letNew


This is my code so far. The output gives me a fraction for only one sequence even tough the list sequenceA has four sequences.



Letter_A = calculate_let(sequenceA, 'A')
print(Letter_A)


output: 0.10273972602739725



I have been trying my hardest to try and solve this problem, but quite honestly don't know where to start.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 4:59

























asked Nov 20 at 23:01







user10682340















  • 4




    Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
    – khelwood
    Nov 20 at 23:03














  • 4




    Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
    – khelwood
    Nov 20 at 23:03








4




4




Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
– khelwood
Nov 20 at 23:03




Please fix your indentation. Badly indented Python is not a Minimal, Complete, and Verifiable example.
– khelwood
Nov 20 at 23:03












2 Answers
2






active

oldest

votes


















1














I think you meant to make a list of them like this:



Notice the list.append method will add an element to the list.



def count_lett(sequences, AminoAcid):
count_lettNew =
for seq in sequences:
count_lettNew.append(seq.count(AminoAcid)/len(seq))
return count_lettNew




As random advice:



Python only uses the TitleCase for class names, so I would choose to write the code like this (keeping the forloop for simplicity):



def count_lett(sequences, amino_acid):
counts = # Although, you may consider something like fracs
# for "fractions" since these aren't actually counts
for seq in sequences:
counts.append(seq.count(amino_acid)/len(seq))
return counts





share|improve this answer































    0














    You could do it in a very readable and concise way using a list comprehension:



    def calc_freqs(sequences, letter):
    return [seq.count(letter)/len(seq) for seq in sequences]

    sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
    letterA = calc_freqs(sequenceA, 'A')
    print(letterA) # -> [0.5714285714285714, 0.0, 0.25, 0.2]





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402898%2fhow-can-i-rewrite-this-to-give-me-a-list-of-answers%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown
























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I think you meant to make a list of them like this:



      Notice the list.append method will add an element to the list.



      def count_lett(sequences, AminoAcid):
      count_lettNew =
      for seq in sequences:
      count_lettNew.append(seq.count(AminoAcid)/len(seq))
      return count_lettNew




      As random advice:



      Python only uses the TitleCase for class names, so I would choose to write the code like this (keeping the forloop for simplicity):



      def count_lett(sequences, amino_acid):
      counts = # Although, you may consider something like fracs
      # for "fractions" since these aren't actually counts
      for seq in sequences:
      counts.append(seq.count(amino_acid)/len(seq))
      return counts





      share|improve this answer




























        1














        I think you meant to make a list of them like this:



        Notice the list.append method will add an element to the list.



        def count_lett(sequences, AminoAcid):
        count_lettNew =
        for seq in sequences:
        count_lettNew.append(seq.count(AminoAcid)/len(seq))
        return count_lettNew




        As random advice:



        Python only uses the TitleCase for class names, so I would choose to write the code like this (keeping the forloop for simplicity):



        def count_lett(sequences, amino_acid):
        counts = # Although, you may consider something like fracs
        # for "fractions" since these aren't actually counts
        for seq in sequences:
        counts.append(seq.count(amino_acid)/len(seq))
        return counts





        share|improve this answer


























          1












          1








          1






          I think you meant to make a list of them like this:



          Notice the list.append method will add an element to the list.



          def count_lett(sequences, AminoAcid):
          count_lettNew =
          for seq in sequences:
          count_lettNew.append(seq.count(AminoAcid)/len(seq))
          return count_lettNew




          As random advice:



          Python only uses the TitleCase for class names, so I would choose to write the code like this (keeping the forloop for simplicity):



          def count_lett(sequences, amino_acid):
          counts = # Although, you may consider something like fracs
          # for "fractions" since these aren't actually counts
          for seq in sequences:
          counts.append(seq.count(amino_acid)/len(seq))
          return counts





          share|improve this answer














          I think you meant to make a list of them like this:



          Notice the list.append method will add an element to the list.



          def count_lett(sequences, AminoAcid):
          count_lettNew =
          for seq in sequences:
          count_lettNew.append(seq.count(AminoAcid)/len(seq))
          return count_lettNew




          As random advice:



          Python only uses the TitleCase for class names, so I would choose to write the code like this (keeping the forloop for simplicity):



          def count_lett(sequences, amino_acid):
          counts = # Although, you may consider something like fracs
          # for "fractions" since these aren't actually counts
          for seq in sequences:
          counts.append(seq.count(amino_acid)/len(seq))
          return counts






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 23:15

























          answered Nov 20 at 23:09









          Stephen Cowley

          1,099417




          1,099417

























              0














              You could do it in a very readable and concise way using a list comprehension:



              def calc_freqs(sequences, letter):
              return [seq.count(letter)/len(seq) for seq in sequences]

              sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
              letterA = calc_freqs(sequenceA, 'A')
              print(letterA) # -> [0.5714285714285714, 0.0, 0.25, 0.2]





              share|improve this answer




























                0














                You could do it in a very readable and concise way using a list comprehension:



                def calc_freqs(sequences, letter):
                return [seq.count(letter)/len(seq) for seq in sequences]

                sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
                letterA = calc_freqs(sequenceA, 'A')
                print(letterA) # -> [0.5714285714285714, 0.0, 0.25, 0.2]





                share|improve this answer


























                  0












                  0








                  0






                  You could do it in a very readable and concise way using a list comprehension:



                  def calc_freqs(sequences, letter):
                  return [seq.count(letter)/len(seq) for seq in sequences]

                  sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
                  letterA = calc_freqs(sequenceA, 'A')
                  print(letterA) # -> [0.5714285714285714, 0.0, 0.25, 0.2]





                  share|improve this answer














                  You could do it in a very readable and concise way using a list comprehension:



                  def calc_freqs(sequences, letter):
                  return [seq.count(letter)/len(seq) for seq in sequences]

                  sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
                  letterA = calc_freqs(sequenceA, 'A')
                  print(letterA) # -> [0.5714285714285714, 0.0, 0.25, 0.2]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 at 6:56

























                  answered Nov 21 at 1:04









                  martineau

                  65.6k988177




                  65.6k988177






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402898%2fhow-can-i-rewrite-this-to-give-me-a-list-of-answers%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'