Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating values of a dictionary by comparing two dictionaries of unequal lengths with similar keys

I have a list of characters from the list character_list = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'] which I am using to track the number of character occurrences in a given string. My approach is to make a dictionary that has each character in character_list as its key and its count as the value. If a character within character_list is not present within string then its value will be None

I have a string which I used to make a dictionary to count the frequency of each character within the string.

from collections import  Counter

character_list = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']

string ='LDPQKLFWWWWWWWWWWWWWWWWWDKIRERNDCEQGHILYKMFPSTRTKRCQTSGGGPHDGPQDLDRELFKLKQMGKDMNTFPNFTFEDPKFE'

string_counts = dict(sorted((Counter(string)).items(), key=lambda tuple_element: tuple_element[0] ) )


string_counts yields :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'C': 2, 'D': 8, 'E': 5, 'F': 7, 'G': 6, 'H': 2, 'I': 2, 'K': 8, 'L': 6, 'M': 3, 'N': 3, 'P': 6, 'Q': 5, 'R': 5, 'S': 2, 'T': 5, 'W': 17, 'Y': 1}


Since not all characters in string are in string_counts, character_list and string_count are of different lengths and won’t have all the same keys. This makes constructing the dictionary difficult.

To get around this I tried making a dictionary of Boolean Values, where if the character is present in both string and character_list the value will be True and None if the character is not present in string in order to make them both the same length. I did that using zip and cycle

from itertools import cycle

bool_dict = dict()

for string_count_letter, char_letter in zip( cycle( string_counts.keys() ), character_list):

    if char_letter in string_counts.keys():
        bool_dict[char_letter] = True
    else :
        bool_dict[char_letter] = None
print(bool_dict)

bool_dict yields:

{'A': None, 'C': True, 'D': True, 'E': True, 'F': True, 'G': True, 'H': True, 'I': True, 'K': True, 'L': True, 'M': True, 'N': True, 'P': True, 'Q': True, 'R': True, 'S': True, 'T': True, 'V': None, 'W': True, 'Y': True}

Then from here I want my final dictionary to be:

dict_i_want = {'A': None, 'C': 2, 'D': 8, 'E': 5, 'F': 7, 'G': 6, 'H': 2, 'I': 2, 'K': 8, 'L': 6, 'M': 3, 'N': 6, 'P': 6, 'Q': 5, 'R': 2, 'S': 5, 'T': 5, 'V': None,'W':17,'Y':1}
}

but using this code that updates bool_dict values if the value is True to the frequency of a character within “string“` gets me a dictionary that mismatches the frequencies to the wrong character:

string_count_values = list(string_counts.values())
bool_values = list(bool_dict.values())
bool_keys = list(bool_dict.keys())

for  string_count_v, bool_v, bool_k in zip(  cycle(string_count_values),bool_values , bool_keys ):

    print(bool_v)
    if bool_v == True :

        bool_dict[bool_k] = string_count_v

print(bool_dict) 
bool_dict{'A': None, 'C': 8, 'D': 5, 'E': 7, 'F': 6, 'G': 2, 'H': 2, 'I': 8, 'K': 6, 'L': 3, 'M': 3, 'N': 6, 'P': 5, 'Q': 5, 'R': 2, 'S': 5, 'T': 17, 'V': None, 'W': 2, 'Y': 8} # this is wrong

#compared to 
dict_i_want = {'A': None, 'C': 2, 'D': 8, 'E': 5, 'F': 7, 'G': 6, 'H': 2, 'I': 2, 'K': 8, 'L': 6, 'M': 3, 'N': 6, 'P': 6, 'Q': 5, 'R': 2, 'S': 5, 'T': 5, 'V': None,'W':17,'Y':1}
}
# this is right

>Solution :

All you need:

from collections import  Counter

character_list = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']

string ='LDPQKLFWWWWWWWWWWWWWWWWWDKIRERNDCEQGHILYKMFPSTRTKRCQTSGGGPHDGPQDLDRELFKLKQMGKDMNTFPNFTFEDPKFE'

c = Counter(string)

dict_i_want = {k: None if k not in c else c[k] for k in character_list}
print(dict_i_want)

Result:

{'A': None, 'C': 2, 'D': 8, 'E': 5, 'F': 7, 'G': 6, 'H': 2, 'I': 2, 'K': 8, 'L': 6, 'M': 3, 'N': 3, 'P': 6, 'Q': 5, 'R': 5, 'S': 2, 'T': 5, 'V': None, 'W': 17, 'Y': 1}

What I’d prefer:

dict_i_want = {k: 0 if k not in c else c[k] for k in character_list}

And then even this works:

dict_i_want = {k: c[k] for k in character_list}

Because a Counter returns 0 for a key that’s not in it anyway.

(By the way, naming a variable string shadows the Python module string – not used very commonly these days, but you may want to avoid shadowing it all the same and use a more descriptive name for a variable, e.g. sample)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading