简体   繁体   English

通过比较两个长度不等的字典和相似的键来更新字典的值

[英]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 .我有一个列表character_list = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']我用来追踪号码给定string中出现的字符数。 My approach is to make a dictionary that has each character in character_list as its key and its count as the value.我的方法是制作一个字典,将 character_list 中的每个character_list作为键,将计数作为值。 If a character within character_list is not present within string then its value will be None如果string中不存在 character_list 中的character_list ,则其值为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: string_counts 产生:

{'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.由于并非string中的所有字符都在string_counts中,因此character_liststring_count的长度不同,并且不会具有所有相同的键。 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.为了解决这个问题,我尝试制作一个 Boolean 值的字典,如果stringcharacter_list中都存在该字符,则如果string中不存在该字符,则该值将为TrueNone ,以使它们具有相同的长度。 I did that using zip and cycle我使用zipcycle

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: bool_dict产生:

{'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:但是使用此代码更新bool_dict值,如果该值对``string```中字符的频率为True ,则可以得到一个字典,该字典与错误字符的频率不匹配:

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

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.因为Counter为不在其中的键返回 0。

(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, eg sample ) (顺便说一句,命名变量string会隐藏 Python 模块string - 这些天不常用,但您可能希望避免将其全部隐藏并为变量使用更具描述性的名称,例如sample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM