0

Write a function that prints the dictionary in the following manner:

Key : GT - Value : 1
Key : CT - Value : 8


Code:

def occurrences2letters(sequence): 
    dico = {}
    for i in range(len(sequence)-1): 
          key = sequence[i]+sequence[i+1] 
          dico[key] = dico.get(key,0) + 1
     return dico

I got {'AC': 1, 'CC': 3, 'CT': 8, 'TA': 7, 'AG': 7, 'GC': 7, 'CA': 1, 'AT': 2, 'TG': 2, 'GT': 1, 'GA': 1, 'AA': 1, 'TC': 2, 'CG': 1, 'GG': 1, 'TT': 2}

Now I want another code helping me to have the result showed in the beginning. For the moment I have written that part of the code but it doesn't work

def printDico(dico):
      for key in dico:
          print("Key : "+str(key)+" - Value : "+str(dico[key]))
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • What exactly doesn't work? – user2390182 Oct 25 '20 at 10:41
  • I am not able to get the dict in that format Key : GT - Value : 1 Key : CT - Value : 8. – Filipe Wolfrum caeiros Oct 25 '20 at 10:42
  • Do you actually call your function `printDico()`? Because it works - printing all keys and values in the expected format. Give more details as to what exactly is the problem - e.g. do you get error, what get printed, etc. – buran Oct 25 '20 at 10:45
  • I think you should use for key in ['GT, 'CT'] for producing the desired output. – Deepak Tripathi Oct 25 '20 at 10:46
  • Also note that we are not able to comment on `occurrences2letters()` because we have no info as to what your sequence looks like and if the sample `dico` is what is expected – buran Oct 25 '20 at 10:49
  • The info about occurence2letters() is dico = occurrences2letters("ACCTAGCCATGTAGAATCGCCTAGGCTTTAGCTAGCTCTAGCTAGCTG"). In fact I dont know how to call the second part of the code with printDico in order to show the result I want. Should it be separated from the previous code or not separeted ? Could you show me how you got a result @buran – Filipe Wolfrum caeiros Oct 25 '20 at 10:53

1 Answers1

0
def occurrences2letters(sequence): 
    dico = {}
    for i in range(len(sequence)-1): 
        key = sequence[i]+sequence[i+1] 
        dico[key] = dico.get(key,0) + 1
    return dico

def printDico(dico):
    for key in dico:
        print("Key : "+str(key)+" - Value : "+str(dico[key]))

spam = "ACCTAGCCATGTAGAATCGCCTAGGCTTTAGCTAGCTCTAGCTAGCTG"
dico = occurrences2letters(spam)
printDico(dico)

output:

Key : AC - Value : 1
Key : CC - Value : 3
Key : CT - Value : 8
... some output left out for brevity
Key : CG - Value : 1
Key : GG - Value : 1
Key : TT - Value : 2

Your occurrences2letters() returns a dict, you first need to call it, passing your string as argument, then call printDico() passing the dict from the first function as argument.

Note, there are better ways to do what you did, using e.g. collections.Counter, more pythonic way to iterate over key, value pairs of a dict, etc.

import collections
import more_itertools # third-party library, instaled from pypi
spam = "ACCTAGCCATGTAGAATCGCCTAGGCTTTAGCTAGCTCTAGCTAGCTG"

for key, value in collections.Counter(more_itertools.windowed(spam, 2)).items():
    print(f'Key: {"".join(key)} - Value: {value}')
buran
  • 13,682
  • 10
  • 36
  • 61