我想實現在Python字典中搜索特定鍵值(使用正則表達式作爲鍵)的值。使用正則表達式的鍵的Python字典搜索值
例子:
我有像有值的Python字典:
{'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}
我需要尋找價值,其關鍵有 'seller_account'?我寫了一個示例程序,但想知道是否可以做得更好。最主要的原因是我不知道的正則表達式,並錯過了一些東西(像我怎麼設置再重點以「seller_account」):
#!usr/bin/python
import re
my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}
reObj = re.compile('seller_account')
for key in my_dict.keys():
if(reObj.match(key)):
print key, my_dict[key]
~ home> python regular.py
seller_account_number 3433343
seller_account_0 454676
seller_account 454545
如果您必須經常搜索這些鍵的這些部分,那麼您的數據結構可能有問題。 – eumiro