Python Forum
Iterating through a list of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating through a list of strings
#1
HELLO. I AM TRYING TO COUNT G'S AND C'S IN A LIST OF STRINGS AND HAVE THE LOOP PUT OUT THE GC COUNT AND THEN CALCULATE A GC PERCENTAGE. I HAVE TRIED VARIOUS CONFIGURATIONS OF A NESTED LOOP AS WELL AS TRYING TO FIGURE OUT HOW TO SOLVE IT USING A FUNCTION, AND AM JUST STUCK. ANY HELP WOULD BE GREATLY APPRECIATED. I AM STUCK ON PART 2/3.


print("\n########## PART 3 ##########\n")

#PART 3 -  Iterate through a list of strings, calculate value, write to file

#(1) Iterate through the list of sequences
#(2) Count G's and C's for each sequence number in a list.
#(3) Calculate the GC Percentage for the sequence nmer.
#(4) Write the sequence and its GC percentage (separated by a "Tab")
#    to a file named:
#       gc_output.txt

#For example if given the following sequence list
#seq_list=["AAUG","AGCG","GCUA"]

#The loop would write the following to the file gc_output.txt
#AAUG    25.0
#AGCG    75.0
#GCUA    50.0

#== PART 3 ==

#seq_list=["AAUG","AGCG","GCUA"]
#seq_list=["AAUGGAGACU","AGCGCCCC","GCUAAAAAAU"]

#(1)
seqs=["AAUG","AGCG","GCUA"]
for element in seqs:
    print(element)

#(2)
gc_count=0
total=len(seqs)
for seq in seqs:
    for x in seq:
        if x == "G" or x == "C":
            gc_count+=1
            print(gc_count)

#c_count=0
#seq=["AAUG"]
#for x in seq:
#    if x =="G" or x =="C":
#        gc_count+=1
#        print(gc_count)

#def gc_count(string)

#Doing a for loop for one of the three sequences
seq="AGGC"
gc_count=0
for b in seq:
    if b=="G" or b=="C":
        gc_count+=1
    else:
        gc_count+=0
print(gc_count)

gc_count=0
seq_list=["AAUG", "AGCG", "GCUA"]
def gc_calc(seq_list):
    for b in seq_list:
        if re.search(g,c):
            gc_count+=1
        else:
            pass
    return gc_count

            
#Lab 04 Part 3 Work
#(3)
#gc_count=0
#seqs=["AAUG","AGCG","GCUA"]
#total=len(seqs)
#for seq in seqs:
#    for x in seq:
 #       if x == "G" or x == "C":
#            gc_count+=1
#            gc_percentage = gc_count/float(total)
#            print("gc_percentage", gc_percentage)
    
#gc_percentage = gc_count/float(total)
#print("gc_percentage", gc_percentage)


#g=0
#c=0
#t=0
#a=0
#gc_count=0
#a_count=0
#t_count=0
#seq_list=["AAUG","AGCG","GCUA"]
#total=len(seq_list)
#print("total = ", total) #this refers to how many strings are in the list
#for seq in seq_list:
#    if i == "G" or i == "C":
#        gc_count+=1
#        print("gc=", gc_count)
#    elif i == "A":
#        a_count+=1
#        print(a_count)
#    elif i == "T":
#        t_count+=1
#        print(t_count)

#gc_count=0
#seq=["AAUG"]
#for char in seq:
 #   if i == "G" or i =="C":
 #       gc_count+=1
 #       print(gc_count)


#for x in seq_list:
#    if "C" in x:
#        c+=1    
#    elif "G" in x:
#        g+=1
#    elif "A" in x:
#        a+=1    
#    elif "T" in x:
#        t+=1

#print ("C=%total, G=%total, A=%total, T=%total" %(c,g,a,t))

#gc_content=(g+c)*100/(a+t+g+c)

#print ("gc_content= %f" %(gc_content))
Reply
#2
Please, don't use ALL CAPS! It's yelling
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
  How to read module/class from list of strings? popular_dog 1 422 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 647 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,029 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Delete list while iterating shantanu97 1 1,866 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,195 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  python3: iterating through list not working wardancer84 3 2,302 Jul-08-2020, 04:30 PM
Last Post: DPaul
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,140 Jun-02-2020, 05:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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