-1

I have an empty list and should append any keys to the list words if the key's corresponding value in freq is equal to m.

I have the inputs ACGTTGCATGTCGCATGATGCATGAGAGCT (for text) and 4 (for k).

What I need to do is check where something is in the list:

>>>> words = [ ] 

The above has a frequency value equal to the maximum value, and if it does, appends "key" to it.

The output I am supposed to get is CATG GCAT.

If you are familiar with the genomic data science course from UCSD, you will probably know this problem.

This is the code:

# Input:  A string Text and an integer k
# Output: A list containing all most frequent k-mers in Textdef 
>>>> FrequentWords(Text, k):
>>>> words = []
>>>> freq = FrequencyMap(Text, k)
>>>> m = max(freq.values())
>>>> for key in freq:
        # add each key to words whose corresponding frequency value is equal to m ( this is the part I am struggling with)
>>>> return words
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
NinaY
  • 1
  • 3
  • 2
    Start by formatting your code. It is invalid. – DYZ Aug 17 '18 at 20:12
  • 2
    Possible duplicate of [Getting key with maximum value in dictionary?](https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – nofinator Aug 17 '18 at 20:14

2 Answers2

0

If what you're asking for is a function that takes in a string input INPUT, a min frequency m and returns every char in that string that has a frequency above m then here you go.

>>> def FrequentWords(INPUT, m):
...     counted = collections.Counter(INPUT)
...     payload = []
...     for i in counted:
...         letter_count = counted[i]
...         if letter_count > m:
...             payload.append(i)
...     return payload
arshbot
  • 12,535
  • 14
  • 48
  • 71
0

Python supplies a couple of good features to support these common operations. The Counter type (a special sort of dict) will give you the frequencies; a simple filter will help you return the list.

from collections import Counter

def FrequentWords(Text, k):
    # Build a dict of frequencies in the input
    freq = collections.Counter(Text)

    # Build a list of words whose frequencies are at least the given threshold, k
    words = [word for word in freq if freq[word] >= k]

    return words

This works assuming that Text is an iterable (string of characters, list of strings, tuple, etc.) of the things you want to count. If it's a large string containing an entire paragraph (sequence of letters, rather than divided into words), then you will need to extract words from it, such as

word_list = Text.split()

... and then operate on word_list

Prune
  • 76,765
  • 14
  • 60
  • 81