iT邦幫忙

0

如何讓一字串判斷是否包含python dict字典中的KEY值?

dict={
'abc.123':01,
'def.456':02
}

那麼假設我有一個字串key='ccc.abc.123',但也想要01這個value的值,
要如何設判斷式呢?
原本是使用完全符合的

if key in list(dict.keys())
看不懂你的問題 你想要怎樣符合
你想要 key 符合 abc.123 就取 value ?
for k in dict.keys():
if k in key:
print(dict[k])
Huiicat iT邦新手 4 級 ‧ 2020-04-15 15:12:55 檢舉
不好意思表達不清楚,因為用 in 就必須完全符合'abc.123'這個key,但我希望ccc.abc.123也可以符合abc.123,因為他包含abc.123
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
listennn08
iT邦高手 5 級 ‧ 2020-04-15 15:50:09
最佳解答

就你的問題
反過來

key = 'ccc.abc.123'
for x in dict.keys():
    if x in key:
        print(x)
        print(dict[x])

# output: abc.123
#         01

我要發表回答

立即登入回答