Reputation: 135
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 :
{'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
Upvotes: 0
Views: 65
Reputation: 31319
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
)
Upvotes: 1