我有一個字典 disease_dict ,其中包含串列元素中的值。我想獲取特定鍵的鍵和值,然后檢查該值(作為子字串)是否存在于其他鍵中并獲取所有鍵 --> 值對。
例如,這是字典。我想看看字典中是否存在“Stroke”或“stroke”,然后匹配該鍵的值是否是其他值的子字串(如“C10.228.140.300.775”存在于“C10.228.140.300.275”中.800', 'C10.228.140.300.775.600')
'Stroke': ['C10.228.140.300.775', 'C14.907.253.855'], 'Stroke, Lacunar': ['C10.228.140.300.275.800', 'C10.228.140.300.775.600', 'C14.907.253.329.800', 'C14.907.253.855.600']
我有以下代碼行用于獲取特定術語的鍵和值。
#extract all child terms
for k, v in dis_dict.items():
if (k in ['Glaucoma', 'Stroke']) or (k in ['glaucoma', 'stroke']):
disease = k
tree_id = v
print (disease, tree_id)
else:
disease = ''
tree_id = ''
continue
非常感謝任何幫助!
uj5u.com熱心網友回復:
下面的代碼應該做你想要實作的目標:
dis_dict = {
'Stroke': ['C10.228.140.300.775', 'C14.907.253.855'],
'Stroke, Lacunar': ['C10.228.140.300.275.800', 'C10.228.140.300.775.600', 'C14.907.253.329.800', 'C14.907.253.855']
}
dict_already_printed = {}
for k, v in dis_dict.items():
if ( k.lower() in ['glaucoma', 'stroke'] ):
disease = k
tree_id = v
output = None
for c_code_1 in tree_id:
for key, value in dis_dict.items():
for c_code_2 in value:
if c_code_1 in c_code_2:
if f'{disease} {tree_id}' != f'{key} {value}':
tmp_output = f'{disease} {tree_id}, other: {key} {value}'
if tmp_output not in dict_already_printed:
output = tmp_output
print(output)
dict_already_printed[output] = None
if output is None:
output = f'{disease} {tree_id}'
print(output)
else:
disease = ''
tree_id = ''
continue
所以用字典的另一個資料測驗它,看看它是否按預期作業。它僅在完全匹配的情況下列印:
Stroke ['C10.228.140.300.775', 'C14.907.253.855'], other: Stroke, Lacunar ['C10.228.140.300.275.800', 'C10.228.140.300.775.600', 'C14.907.253.329.800', 'C14.907.253.855']
或者如果沒有發現其他疾病(更改字典值以避免匹配)只有找到的疾病:
Stroke ['C10.228.140.300.775', 'C14.907.253.855']
uj5u.com熱心網友回復:
您有一個很好的起點,并且您可能已經知道,您需要處理拆分它的密鑰。您可以這樣做:
disease_dict = { 'Stroke': ['C10.228.140.300.775', 'C14.907.253.855'], 'Stroke, Lacunar': ['C10.228.140.300.275.800', 'C10.228.140.300.775.600', 'C14.907.253.329.800', 'C14.907.253.855.600'], 'Flue' : ['C10.228.140.300.780'] }
for k, v in disease_dict.items():
tmp = ''.join(x for x in k if x.isalpha() or x == '-' or x == ' ')
tmpKey = tmp.split(' ')
for tk in tmpKey:
if tk.capitalize() in ['Stroke', 'Glaucoma']:
print(k, v, end= ' ') # To remove the new line ending
print(notable_diseases)
首先,我們使用這一行洗掉不必要的字符:
tmp = ''.join(x for x in k if x.isalpha() or x == ' ' or x == '-')
它只保留字母字符、空格和破折號。因為我不知道你的病是什么樣子的,所以我只保留了那些字符(下一行需要空格)。創建這個新的格式化鍵后,我們用空格分割它,然后比較子字串。
tmpKey = tmp.split(' ')
一旦tmpKey完成,我們回圈它以檢查您想要的疾病是否屬于原始密鑰。
for tk in tmpKey:
if tk.capitalize() in ['Stroke', 'Glaucoma']:
print(k, v, end= ' ') # To remove the new line ending
tk.capitalize()用于將第一個字母大寫,因此您不必檢查單詞的兩種形式。
最后,運行上面的腳本后,我們得到了:
Stroke ['C10.228.140.300.775', 'C14.907.253.855'] Stroke, Lacunar ['C10.228.140.300.275.800', 'C10.228.140.300.775.600', 'C14.907.253.329.800', 'C14.907.253.855.600']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526863.html
標籤:Python熊猫字典子串
上一篇:將字典項交叉參考到資料框中的值
