使用通配符键匹配遍历字典 [英] Iterating over dictionary with wildcard key matching

查看:40
本文介绍了使用通配符键匹配遍历字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历字典以检索与键的模式 npi[0-9]+ 匹配的所有值.这在 Python 中可行吗?

I am trying to iterate over a dictionary in order to retrieve all values that match the pattern npi[0-9]+ for the key. Is this possible in Python?

for data in input_file:
    print(data['npi0'])
    print(data['npi1'])
    ....

推荐答案

您只需遍历您的字典,并对键进行模式匹配.

You just iterate over your dictionary, and do a pattern match on the key.

for key in dict:
    if re.match(r'npi[0-9]+', key):
        print(dict[key])

这篇关于使用通配符键匹配遍历字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆