[英]Updating values of a dictionary by comparing two dictionaries of unequal lengths with similar keys
我有一個列表character_list = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']
我用來追蹤號碼給定string
中出現的字符數。 我的方法是制作一個字典,將 character_list 中的每個character_list
作為鍵,將計數作為值。 如果string
中不存在 character_list 中的character_list
,則其值為None
我有一個字符串,我用它來制作字典來計算字符串中每個字符的頻率。
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 產生:
{'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}
由於並非string
中的所有字符都在string_counts
中,因此character_list
和string_count
的長度不同,並且不會具有所有相同的鍵。 這使得構建字典變得困難。
為了解決這個問題,我嘗試制作一個 Boolean 值的字典,如果string
和character_list
中都存在該字符,則如果string
中不存在該字符,則該值將為True
和None
,以使它們具有相同的長度。 我使用zip
並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
產生:
{'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}
然后從這里我希望我的最終字典是:
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}
}
但是使用此代碼更新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
一切你需要的:
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)
結果:
{'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}
我更喜歡什么:
dict_i_want = {k: 0 if k not in c else c[k] for k in character_list}
然后即使這樣也有效:
dict_i_want = {k: c[k] for k in character_list}
因為Counter
為不在其中的鍵返回 0。
(順便說一句,命名變量string
會隱藏 Python 模塊string
- 這些天不常用,但您可能希望避免將其全部隱藏並為變量使用更具描述性的名稱,例如sample
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.