Search code examples
listpython-3.xsubtotalfastq

How do combine a random amount of lists in python


I'm working on a program that reads trough a FASTQ file and gives the amount of N's per sequence in this file. I managed to get the number of N's per line and I put these in a list. The problem is that I need all the numbers in one list to sum op the total amount of N's in the file but they get printed in their own list.

C:\Users\Zokids\Desktop>N_counting.py test.fastq
[4]
4
[3]
3
[5]
5 

This is my output, the List and total amount in the list. I've seen ways to manually combine lists but one can have hundreds of sequences so that's a no go.

def Count_N(line):
    '''
    This function takes a line and counts the anmount of N´s in the line
    '''
    List = []
    Count = line.count("N") # Count the amount of N´s that are in the line returned by import_fastq_file
    List.append(int(Count))

    Total = sum(List)
    print(List)
    print(Total)

This is what I have as code, another function selects the lines.

I hope someone can help me with this. Thank you in advance.


Solution

  • Looks from your code you send one line each time you call count_N(). List you declared is a local list and gets reinitialized when you call the function each time. You can declare the list global using:

    global List =[]
    

    I think you will also need to declare the list outside function in order to access it globally.

    Also it would be better if you Total the list outside the function. Right now you are summing up list inside function. For that you will need to match indentation with the function declaration.