count by number from the same number

Asked 1 years ago, Updated 1 years ago, 71 views

Inside the CHR, it looks like 1 1 1 1 1 1 2 2 2 ... 22 22 X X ... Y ... MT MT MT .I'd like to count the number by number.I don't know what to do because there are letters as well as numbers.

import sys
importos

with open('test.vcf', 'r') as file:
    lines=file.read().split('\n')

    For line in lines:
        a=line.split('\t')
        CHR = a [0]

python python3 bioinformatics

2022-09-30 16:12

1 Answers

Use the collections.Counter.

collections---Container data types—Python 3.7.3 Documentation

This is a subclass of dict that counts the number of appearances for each key of an element, and is exactly the class that exists for that purpose.

You should be able to verify the operation with the following code:

import sys
importos
from collections import Counter

chars = [ ]
with open('test.vcf', 'r') as file:
    lines=file.read().split('\n')

    For line in lines:
        a=line.split('\t')
        CHR = a [0]
        chars.append (CHR)

c=Counter(chars)
print(c)


2022-09-30 16:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.