Reputation: 1568
I have a python dict and following is an output of the keys of the dict:
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'])
I would like to collect the keys which have 'line'
in them to a python list.
To do so I am using following code-piece:
list = []
for key in es_dict.keys():
if 'line' in key:
list.append(key)
But I am getting following error:
if 'line' in key:
TypeError: argument of type 'type' is not iterable
How to fix this?
Upvotes: 0
Views: 69
Reputation: 8582
It's not an issue with your code, but with how your dict
is created. Some of keys in your dict is types/classes, not instances. I.e. <class 'oemof.solph.blocks.Bus'>
This is a bug, most probably. Take a look into piece of code that's creating this mapping.
Upvotes: 1
Reputation: 107134
Some of the keys of es_dict
are class objects, which are not iterable. You can add a condition to ensure that the key is a string before using the in
operator to check if 'line'
is a substring of it:
if isinstance(key, str) and 'line' in key:
Upvotes: 4