I have this dictionary:
a = {
'car1': ('high cp', 'low fd', 'safe'),
'car2': ('med cp', 'med fd', 'safe'),
'car3': ('low cp', 'high fd', 'safe'),
'taxi1': ('high cp', 'low fd', 'safe', 'med wt'),
'taxi2': ('high cp', 'low fd', 'safe', 'high wt'),
'taxi3': ('high cp', 'low fd', 'safe', 'high wt')
}
From the above dictionary, I want to create a new dictionary that consists only 'car%s'
I'm using this code snippet (from another question)
b = {}
for key in a:
if key == 'car%s'% (range (4)):
print (" %s : %s" % (key, a[key]))
print(b)
It returns {}
I expect to get:
a = {
'car1': ('high cp', 'low fd', 'safe'),
'car2': ('med cp', 'med fd', 'safe'),
'car3': ('low cp', 'high fd', 'safe'),
}
What am I missing here?
If you want to add keys which contain word car
in it, then it will work:
a = {
'car1': ('high cp', 'low fd', 'safe'),
'car2': ('med cp', 'med fd', 'safe'),
'car3': ('low cp', 'high fd', 'safe'),
'taxi1': ('high cp', 'low fd', 'safe', 'med wt'),
'taxi2': ('high cp', 'low fd', 'safe', 'high wt'),
'taxi3': ('high cp', 'low fd', 'safe', 'high wt')
}
b = {}
for key in a:
if 'car' in key:
print (key, a[key])
b[key] = a[key]
print(b)
You're checking the prefix the wrong way and you're not storing the result. You could use str.startswith
and dict comprehension to generate the result:
>>> a = {
... 'car1': ('high cp', 'low fd', 'safe'),
... 'car2': ('med cp', 'med fd', 'safe'),
... 'car3': ('low cp', 'high fd', 'safe'),
... 'taxi1': ('high cp', 'low fd', 'safe', 'med wt'),
... 'taxi2': ('high cp', 'low fd', 'safe', 'high wt'),
... 'taxi3': ('high cp', 'low fd', 'safe', 'high wt')
... }
>>> res = {k: v for k, v in a.items() if k.startswith('car')}
>>> res
{'car2': ('med cp', 'med fd', 'safe'), 'car3': ('low cp', 'high fd', 'safe'), 'car1': ('high cp', 'low fd', 'safe')}
Instead of inserting a number to the format string your current check inserts the range
object there which probably isn't the result you expect:
>>> 'car%s'% (range (4))
'carrange(0, 4)'
You never do anything with the keys you validate but print
them. You need to add them to your new dictionary:
b ={}
for key, val in a.items(): # .iteritems() for Python 2.x users
if key == 'car%s' % (range (4)):
b[key] = val
print(b)
Your code is would still be broken however. You need to make some changes:
"car"
. Forget trying to match the whole string. This entire for loop could be made into a very simple dictionary comprehension:
>>> a = { ... 'car1': ('high cp', 'low fd', 'safe'), ... 'car2': ('med cp', 'med fd', 'safe'), ... 'car3': ('low cp', 'high fd', 'safe'), ... 'taxi1': ('high cp', 'low fd', 'safe', 'med wt'), ... 'taxi2': ('high cp', 'low fd', 'safe', 'high wt'), ... 'taxi3': ('high cp', 'low fd', 'safe', 'high wt') ... } >>> {k: v for k, v in a.items() if k[0:3] == 'car'} {'car2': ('med cp', 'med fd', 'safe'), 'car3': ('low cp', 'high fd', 'safe'), 'car1': ('high cp', 'low fd', 'safe')} >>>
This works:
new_dict = {}
for k in a.keys():
if 'car' in k:
new_dict[k] = a[k]
result:
>>new_dict
{'car1': ('high cp', 'low fd', 'safe'),
'car2': ('med cp', 'med fd', 'safe'),
'car3': ('low cp', 'high fd', 'safe')}
what do you expect from range(4)? It returns [0, 1, 2, 3]
b = {}
for key in range(4):
new_key = "car%s" % key # generate new_key
item = a.get(new_key)
if item is not None:
b[new_key] = item
print (b)
OR you want to get items only starts with car
, read this link https://docs.python.org/3/library/stdtypes.html#str.startswith
b = {}
for key, value in a.items():
if key.startswith("car"):
b[key] = a[key]
print (b)
output
{'car2': ('med cp', 'med fd', 'safe'),
'car3': ('low cp', 'high fd', 'safe'),
'car1': ('high cp', 'low fd', 'safe')}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.