0

my problem follows:

dnaSequence = file2.readline()
    for key in nucleotides:
        keys = " ".join(key)
        value = re.search(f"(?:({key}){{2,}}|({key}))", dnaSequence)
        value = int(len(value.group(0)) / len(key))
        STRs = {key: value}
        print(STRs)

I have to solve this problem by reading a file with a unknow DNA chain, and using a regex to find the biggest repetitions of nucleotides sequences inside that chain. Something such as AAGT, AGTC, TTTTTTTCT, and so on. The above code gave me the following result:

{'AGATC': 4}
{'AATG': 1}
{'TATC': 5}

The dictionary key is the nucleotides chain that I have to look up for. The numbers(values) are the number of times that those nucleotides repeat yourselves consecutively. So, just for enlightening, the first result means my regex found AGATCAGATCAGATCAGATC in a long DNA chain in the file.

And I was expecting for {'AGATC': 4, 'AATG': 1, 'TATC': 5}. How can I merge these 3 different values in one single dictionary?

Viny
  • 21
  • 6

1 Answers1

0

To start by answering the question exactly as stated, you can combine two dictionaries using the update method - this does an in-place edit on the dictionary whose method is being called.

    answer = {}
    for key in nucleotides:
        # ... prepare value variable ...
        STRs = {key: value}
        answer.update(STRs)

But in this case, given that you are only setting one key, if you don't actually need the dictionary STRs for anything, you could just set it directly in the output dictionary.

    answer = {}
    for key in nucleotides:
        # ... prepare value variable ...
        answer[key] = value

By the way, in your code you are not using the variable keys that you are creating.

alani
  • 12,573
  • 2
  • 13
  • 23