4

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?

Community
  • 1
  • 1
dya
  • 195
  • 1
  • 2
  • 16
  • 1
    You are just printing keys with `car` in them, not adding them to another dictionary. – Fejs Dec 15 '16 at 06:38

5 Answers5

6

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)'
niemmi
  • 17,113
  • 7
  • 35
  • 42
3

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)
Nurjan
  • 5,889
  • 5
  • 34
  • 54
1

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:

  • The only prefix you need to check is "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')}
    >>> 
    
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
1

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')}
Roman
  • 8,826
  • 10
  • 63
  • 103
0

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')}
minji
  • 512
  • 4
  • 16