相关疑难解决方法(0)

过滤python字典中的项,其中键包含特定字符串

我是一名C编码员,用python开发一些东西.我知道该怎么做在C以下(因此在应用于蟒类似C的逻辑),但我不知道做什么的"Python的"事情是这样的.

我有一个字典d,我想操作一个项目的子集,只有那些键(字符串)包含一个特定的子字符串.

即C逻辑将是:

for key in d:
    if filter_string in key:
        # do something
    else
        # do nothing, continue
Run Code Online (Sandbox Code Playgroud)

我想象python版本会是这样的

filtered_dict = crazy_python_syntax(d, substring)
for key,value in filtered_dict.iteritems():
    # do something
Run Code Online (Sandbox Code Playgroud)

我在这里发现了很多关于过滤字典的帖子,但找不到一个涉及到这个的字典.

我的字典没有嵌套,我正在使用python 2.7

python dictionary filtering python-2.7

78
推荐指数
6
解决办法
9万
查看次数

标签 统计

dictionary ×1

filtering ×1

python ×1

python-2.7 ×1