python-3.x 如何在字典中选择键的子集?[duplicate]

rta7y2nd  于 2023-01-27  发布在  Python
关注(0)|答案(3)|浏览(144)
    • 此问题在此处已有答案**:

How to filter a dictionary according to an arbitrary condition function?(7个答案)
21小时前关门了。
我有本大字典

{
        "configGlossary:installationAt": "Philadelphia, PA",
        "configGlossary:adminEmail": "ksm@pobox.com",
        "configGlossary:poweredBy": "Cofax",
        "configGlossary:poweredByIcon": "/images/cofax.gif",
        "configGlossary:staticPath": "/content/static",
        "templateProcessorClass": "org.cofax.WysiwygTemplate",
        "templateLoaderClass": "org.cofax.FilesTemplateLoader",
        "templatePath": "templates",
        "templateOverridePath": "",
        "defaultListTemplate": "listTemplate.htm",
        "defaultFileTemplate": "articleTemplate.htm",
        "useJSP": false,
        "jspListTemplate": "listTemplate.jsp",
        "jspFileTemplate": "articleTemplate.jsp",
        "cachePackageTagsTrack": 200,
        "cachePackageTagsStore": 200,
        "cachePackageTagsRefresh": 60,
        "cacheTemplatesTrack": 100,
        "cacheTemplatesStore": 50,
        "cacheTemplatesRefresh": 15,
        "cachePagesTrack": 200,
        "cachePagesStore": 100,
        "cachePagesRefresh": 10,
        "cachePagesDirtyRead": 10,
        "searchEngineListTemplate": "forSearchEnginesList.htm",
        "searchEngineFileTemplate": "forSearchEngines.htm",
        "searchEngineRobotsDb": "WEB-INF/robots.db",
        "useDataStore": true,
        "dataStoreClass": "org.cofax.SqlDataStore",
        "redirectionClass": "org.cofax.SqlRedirection",
        "dataStoreName": "cofax",
        "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
        "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
        "dataStoreUser": "sa",
        "dataStorePassword": "dataStoreTestQuery",
        "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
        "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
        "dataStoreInitConns": 10,
        "dataStoreMaxConns": 100,
        "dataStoreConnUsageLimit": 100,
        "dataStoreLogLevel": "debug",
        "maxUrlLength": 500}

例如,我需要一个包含reduce键值对的字典

{       
        "cachePackageTagsTrack": 200,
        "cachePackageTagsStore": 200,
        "cachePackageTagsRefresh": 60,
        "cacheTemplatesTrack": 100,
        "cacheTemplatesStore": 50,
        "cacheTemplatesRefresh": 15,
        "cachePagesTrack": 200,
        "cachePagesStore": 100,
        "cachePagesRefresh": 10,
        "cachePagesDirtyRead": 10
}

没有字典方法也能做到这一点。
我有一个循环中运行的大型相似数据集
我正在寻找快速有效的方法来减少字典

wgeznvg7

wgeznvg71#

作为一种快速的替代方法,您可以将要保留的密钥存储在某个地方,然后循环使用它们:

keys_list = ["cachePackageTagsTrack",
             "cachePackageTagsStore",
             "cachePackageTagsRefresh",
             "cacheTemplatesTrack",
             "cacheTemplatesStore",
             "cacheTemplatesRefresh",
             "cachePagesTrack",
             "cachePagesStore",
             "cachePagesRefresh",
             "cachePagesDirtyRead"]

my_new_dict = {key: my_dict.get(key, "") for key in keys_list}
qltillow

qltillow2#

您可以使用字典解析来创建一个新字典,其中只包含要保留的键值对。

# keys to keep
keys_to_keep = ["cachePackageTagsTrack", "cachePackageTagsStore", "cachePackageTagsRefresh", 
                "cacheTemplatesTrack", "cacheTemplatesStore", "cacheTemplatesRefresh", 
                "cachePagesTrack", "cachePagesStore", "cachePagesRefresh", "cachePagesDirtyRead"]

# create new dictionary with only the keys to keep
reduced_dict = {key: large_dict[key] for key in keys_to_keep if key in large_dict}
cld4siwp

cld4siwp3#

另一个解决方案是,如果你不想创建一个单独的键列表,那么在创建简化dict时,把键过滤成你感兴趣的、具有共同模式的键,在本例中,它们都以子字符串cache开头:

reduced_dict = {key: value for key, value in my_dict.items() if key.startswith('cache')}

相关问题