按字典键的第一个字母对字典进行排序

2024-05-15 05:41:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个读取整个DNA基因组的代码,并返回一个字典所有8-引物及其位置,我想循环浏览这本字典,并根据它们以a,T,G和C开头的字母将这些密码子排序到其他4个字典中。 但我不知道如何检查每把钥匙的第一个字母

这是我的代码:

"""
Generating all the possible 8-codon primers.
saving them in a text file with their locations.
"""

import csv
##MAIN FUNCTION:
def k_mer(Text, k):
    dictionary = {}
    for i in range (len(Text) - k + 1):
        if(Text[i: i+k] in dictionary):
            dictionary[Text[i: i+k]].append(i)
        else:
            dictionary[Text[i: i+k]] = [i]
    return dictionary

##INPUT:
# open the file with the original sequence
myfile = open('Vibrio_cholerae.txt')

# set the file to the variable Text to read and scan
Text = myfile.read()
result = k_mer(Text.strip(), 8)

with open("result.txt","w") as f:
    from collections import Counter
    wr = csv.writer(f,delimiter=":")
    wr.writerows(Counter(result).items())

here is a picture of the result dictionary that i want to sort by letter.


Tags: csvthe代码textinimportdictionary字典

热门问题