简体   繁体   中英

How to output a dictionary that finds maximum frequency values from a list?

I'm trying to create a code that loops through a string of Text, with a k-mer, and outputs the most frequent patterns. (Please note I am also a beginner coder). I am using Juptyer notebook, and whenever I try to run my code, there is no output. (Maybe it's something to do with Juptyer)? For example, let's say my Text is GATGATATAC, and k-mer is 3, then I would like to output the most frequent patterns and show that as an indexed dictionary with the frequencies of each 3-letter pattern word.

    def FrequentWords(Text, k):
        words = []
        freq = FrequencyMap(Text, k)
        m = max(freq.values())
        for key in freq:
            if freq[key] == m:
                words.append(key)
    Text = 'GATTACCGACGTATGCTACTCCGATACGATAT'
    k = 3
    return words

I am not really sure where I should define Text and k, in order to test the code (which also could be an issue). As I said, I am using Juptyer notebook and when I press run nothing happens.

I believe you meant this:

def FrequentWords(Text, k):
    words = []
    freq = FrequencyMap(Text, k)
    m = max(freq.values())
    for key in freq:
        if freq[key] == m:
            words.append(key)
    return words

Text = 'GATTACCGACGTATGCTACTCCGATACGATAT'
k = 3
FrequentWords(Text, k)

Also, you can simplify your function a bit with a list comprehension:

def FrequentWords(Text, k):
    freq = FrequencyMap(Text, k)
    m = max(freq.values())
    return [key for key in freq if freq[key] == m]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM