<jupyter_start><jupyter_text>Introduction to Python for Bioinformatics - available at https://github.com/kipkurui/Python4Bioinformatics.  Dictionaries Dictionaries are mappings between keys and items stored in the dictionaries. Unlike lists and tuples, dictionaries are unordered. Alternatively one can think of dictionaries as sets in which something stored against every element of the set. They can be defined as follows: To define a dictionary, equate a variable to { } or dict()<jupyter_code>d = dict() # or equivalently d={}
print(type(d))
d['abc'] = 3
d[4] = "A string"
print(d)<jupyter_output><class 'dict'>
{'abc': 3, 4: 'A string'}<jupyter_text>As can be guessed from the output above. Dictionaries can be defined by using the `{ key : value }` syntax. The following dictionary has three elements<jupyter_code>d = { 1: 'One', 2 : 'Two', 100 : 'Hundred'}
len(d)
d<jupyter_output><empty_output><jupyter_text>Now you are able to access 'One' by the index value set at 1<jupyter_code>d['one']<jupyter_output><empty_output><jupyter_text>There are a number of alternative ways for specifying a dictionary including as a list of `(key,value)` tuples.To illustrate this we will start with two lists and form a set of tuples from them using the **zip()** functionTwo lists which are related can be merged to form a dictionary.<jupyter_code>names = ['Two', 'One', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]
[ (name,number) for name,number in zip(names,numbers)] # create (name,number) pairs
d1 = {}
for name,number in zip(names,numbers):
    d1[name] = number
d1<jupyter_output><empty_output><jupyter_text>Now we can create a dictionary that maps the name to the number as follows.<jupyter_code>a1 = dict((name,number) for name,number in zip(names,numbers))
print(a1)<jupyter_output>{'Two': 1, 'One': 2, 'Three': 3, 'Four': 4, 'Five': 5}<jupyter_text>Note that the ordering for this dictionary is not based on the order in which elements are added but on its own ordering (based on hash index ordering). It is best never to assume an ordering when iterating over elements of a dictionary."Dictionaries are **insertion ordered[1]**. As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior[1]).As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature."By using tuples as indexes we make a dictionary behave like a sparse matrix:<jupyter_code>matrix={ (0,1): 3.5, (2,17): 0.1}
matrix[2,2] = matrix[0,1] + matrix[2,17]
print(matrix)<jupyter_output>{(0, 1): 3.5, (2, 17): 0.1, (2, 2): 3.6}<jupyter_text>Dictionary can also be built using the loop style definition.<jupyter_code>a2 = { name : len(name) for name in names}
print(a2)<jupyter_output>{'Two': 3, 'One': 3, 'Three': 5, 'Four': 4, 'Five': 4}<jupyter_text>Built-in Functions The **len()** function and **in** operator have the obvious meaning:<jupyter_code>print("a1 has",len(a1),"elements")
print("One is in a1 is",'One' in a1,", but Zero in a1 is", 'Zero' in a1)<jupyter_output>a1 has 5 elements
One is in a1 is True , but Zero in a1 is False<jupyter_text>**clear( )** function is used to erase all elements.<jupyter_code>a2.clear()
print(a2)<jupyter_output>{}<jupyter_text>**values( )** function returns a list with all the assigned values in the dictionary. (Acutally not quit a list, but something that we can iterate over just like a list to construct a list, tuple or any other collection):<jupyter_code>a2=a1.values()
list(a2)
[ v for v in a1.values() ]<jupyter_output><empty_output><jupyter_text>**keys( )** function returns all the index or the keys to which contains the values that it was assigned to.<jupyter_code>a1.items()
{ k for k in a1.keys() }<jupyter_output><empty_output><jupyter_text>**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used - except that the ordering has been 'shuffled' by the dictionary.<jupyter_code>",  ".join( "%s = %d" % (name,val) for name,val in a1.items())<jupyter_output><empty_output><jupyter_text>**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value.<jupyter_code>val = a1.pop('Four')
print(a1)
print("Removed",val)
a1['Two'] = ''
?a1.pop('Four')
for key in a1.keys():
    print(key, a1[key])<jupyter_output>2 One
3 Three
5 Five
 Two<jupyter_text>Exercise- Using strings, lists, tuples and dictionaries concepts, find the reverse complement of AAAAATCCCGAGGCGGCTATATAGGGCTCCGGAGGCGTAATATAAAAAlgorithm:1. Store the DNA in a string    - Use it as a string    - Convert it to a list2. Reverse the dna string    - reverse method on lists    - Slice methods on lists    - use negative indexing (slicing) on a string or list3. Complement:    - For a string, we can use replace    - Use conditionals to replace an empty list    - use a DNA complement dictionary      Using Strings<jupyter_code>dna = 'AAAAATCCCGAGGCGGCTATATAGGGCTCCGGAGGCGTAATATAAAAG'
#reversed_dna = ''.join([dna[-i] for i in range(1,len(dna)+1)])
reversed_dna = dna[::-1]
reversed_dna = reversed_dna.replace('A','t').replace('C','g').replace('G','c').replace('G','c').replace('T','a')
reversed_dna.upper()<jupyter_output><empty_output><jupyter_text>Using Conditionals<jupyter_code>comp_dna = []
for nuc in dna:
    if nuc == 'A':
        comp_dna.append('T')
    elif nuc == 'T':
        comp_dna.append('A')
    elif nuc == 'C':
        comp_dna.append('G')
    elif nuc == 'G':
        comp_dna.append('C')
    else:
        comp_dna.append(nuc)
#comp_dna.reverse()
''.join(comp_dna)
revCompDNA = ''
for nuc in comp_dna:
    revCompDNA = revCompDNA + nuc
revCompDNA<jupyter_output><empty_output><jupyter_text>Using the dictionary<jupyter_code>dna
dna = 'AAAAATCCCGAGGCGGCTATATAGGGCTCCGGAGGCGTAATATAAAAG'
rev_dna_dict = {'A':'T', 'C':'G', 'G':'C', 'T':'A'}
comp_dna = ''
for nuc in dna:
    comp_dna = rev_dna_dict[nuc] + comp_dna
print(comp_dna)<jupyter_output>CTTTTATATTACGCCTCCGGAGCCCTATATAGCCGCCTCGGGATTTTT
{'fraction_non_alphanumeric': 0.118099121706399, 'fraction_numerical': 0.01631116687578419, 'mean_word_length': 5.357926221335992, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '22314', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2111, 'n_words': 748, 'path': 'Notebooks/04.ipynb', 'repo_name': 'Aixa98/Bioinformatics-Intro', 'size': '6376'}