我仍然在掌握Python中的字典。通過使用正則表達式還是使用startswith函數,是否可以通過字典的字典引用Key?Python字典的字典正則表達式在鍵上
我有以下格式的字典詞典,我正在嘗試對'AreaOfInterest1','AreaOfInterest2','AreaOfInterest3'等進行一般性搜索。我將其作爲一般參考,然後將相關列表值[ 0]來執行操作。
我的類型的字典詞典看起來像......
FeatureDict = {
"MATCHcode96":{
'ID':'MATCHcode96',
'AreaOfInterest1':['String 1','%',0],
'AreaOfInterest2':['String 2','Avg',[]],
'AreaOfInterest3':['String 3','Avg',[]],
},
"MATCHcode9F":{
'ID':'MATCHcode9F',
'AreaOfInterest1':['String 1','%',0],
'AreaOfInterest2':['String 2','Avg',[]],
},
}
那種功能我找的是要麼使用startswith(如下圖所示),或以其他方式嘗試正則表達式..
for dict in FeatureDict:
if FeatureDict[dict]['ID'] in mypkt.Text:
if FeatureDict[dict][startswith.('AreaOfInterest')][0] in mypkt.Text:
print 'found'
謝謝。
我會建議使用['any'](https://docs.python.org /3/library/functions.html#any)功能。 (FeatureDict [dict])中的任何(FeatureDict [dict] [k] .startswith('AreaOfInterest')和mypkt.Text中的FeatureDict [dict] [k] [0]:print'found''。免責聲明:未經測試。而使用任何方法的缺點是你不能保存你找到的值,但它對於簡單的「找到」條件很有用。 – Shashank
呵呵,'dict'不是一個好的變量名,因爲它會隱藏相同名字的內置類型。 – Shashank