我有一个python字典,下面是该字典的键的输出:
{
"dependencies": {
"@date-io/date-fns": "^1.0.1",
"@material-ui/core": "^1.4.0",
"@material-ui/icons": "^1.0.0",
"@rails/webpacker": "3.5",
"assert": "^1.4.1",
"axios": "^0.18.0",
"babel-core": "^6.26.3",
"babel-eslint": "8.2.5",
"babel-polyfill": "^6.26.0",
"babel-preset-react": "6.24.1",
"bootstrap": "^4.2.1",
"caniuse-lite": "^1.0.30000926",
"classnames": "^2.2.6",
"connected-react-router": "4.5.0",
"crypto-js": "^3.1.9-1",
"date-fns": "2.0.0-alpha.21",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-prettier": "^3.0.0",
"final-form": "^4.11.0",
"history": "^4.7.2",
"i18n-js": "3.1.0",
"jquery": "3.3.1",
"json-loader": "0.5.7",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.11",
"marked": "^0.6.0",
"material-ui": "^0.20.2",
"material-ui-pickers": "^2.1.0",
"normalizr": "^3.3.0",
"popper.js": "^1.14.6",
"prettier": "^1.15.3",
"promise-polyfill": "^8.1.0",
"prop-types": "15.6.2",
"query-string": "6.2.0",
"ra-core": "^2.5.2",
"ra-language-german": "^2.1.1",
"ramda": "^0.26.1",
"react": "^16.6.3",
"react-admin": "^2.1.4",
"react-cookie": "^3.0.8",
"react-cookie-consent": "^2.0.0",
"react-dom": "16.4.1",
"react-final-form": "^3.7.0",
"react-redux": "5.1.1",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-scrollchor": "^6.0.0",
"recompose": "^0.30.0",
"redux": "^4.0.1",
"redux-form": "^7.4.2",
"redux-persist": "^5.10.0",
"redux-saga": "^0.16.2",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"svg-sprite-loader": "^3.8.0",
"webpack": "^3.12.0",
"whatwg-fetch": "^3.0.0",
"yaml-loader": "0.5.0"
},
"devDependencies": {
"dotenv": "^6.2.0",
"enzyme": "3.8.0",
"enzyme-adapter-react-16": "1.7.1",
"enzyme-to-json": "3.3.5",
"eslint": "5.11.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-import-resolver-webpack": "^0.10.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "7.12.3",
"expect": "^23.6.0",
"jest": "23.2.0",
"redux-mock-store": "^1.5.3",
"stylelint": "^9.9.0",
"stylelint-config-standard": "^18.2.0",
"stylelint-declaration-use-variable": "^1.7.0",
"stylelint-order": "^2.0.0",
"webpack-dev-server": "2.11.2"
},
}
我想将其中包含type(es_dict)
<class 'dict'>
type(es_dict.keys())
<class 'dict_keys'>
es_dict.keys()
dict_keys(['b_Biomass_Mid', <class 'oemof.solph.blocks.Bus'>,
'b_Coal_Mid', 'b_Gas_Mid', 'b_Lignite_Mid', 'b_Elec_Mid',
's_Biomass_Mid', <class 'oemof.solph.blocks.Flow'>,
's_Coal_Mid', 's_Gas_Mid', 's_Lignite_Mid', 'rs_Hydro_Mid',
<class 'oemof.solph.blocks.InvestmentFlow'>, 'rs_Solar_Mid',
'rs_Wind_Mid', 'pp_Biomass_Mid', <class 'oemof.solph.blocks.Transformer'>,
'pp_Coal_Mid', 'pp_Gas_Mid', 'pp_Lignite_Mid', 'Elec_Mid',
'storage_Elec_Mid', 'b_Biomass_South', 'b_Coal_South',
'line_Mid_North', 'line_North_South', 'line_Mid_South'])
的键收集到python列表中。
为此,我正在使用以下代码段:
'line'
但是我遇到以下错误:
list = []
for key in es_dict.keys():
if 'line' in key:
list.append(key)
该如何解决?
答案 0 :(得分:4)
es_dict
的某些键是类对象,不可迭代。您可以添加条件以确保键是字符串,然后再使用in
运算符检查'line'
是否为其子字符串:
if isinstance(key, str) and 'line' in key:
答案 1 :(得分:1)
这与您的代码无关,而与dict
的创建方式有关。字典中的某些键是 types / classes ,而不是实例。即<class 'oemof.solph.blocks.Bus'>
这很可能是一个错误。查看创建此映射的代码段。