Looking for a list item when the list is in the dictionary?

This is homework, so I do not expect an answer, just a point in the right direction.

In python, I have a dictionary that looks like this:

{'bike101': ('Road Bike',
             [('WH139', 2),
              ('TR102', 2),
              ('TU177', 2),
              ('FR101', 1),
              ('FB101', 1),
              ('BB101', 1),
              ('GS101', 1)]),
 'bike201': ('Mountain Bike',
             [('WH239', 2),
              ('TR202', 2),
              ('TU277', 2),
              ('FR201', 1),
              ('FB201', 1),
              ('BB201', 1),
              ('GS201', 1)]),
 'bike301': ('Racing Bike',
             [('WH339', 2),
              ('TR302', 2),
              ('TU377', 2),
              ('FR301', 1),
              ('FB301', 1),
              ('BB301', 1),
              ('GS301', 1)])}

For example, "Racing Bike" is the name of the product, and a list of pairs (respectively, part, quantity).

I need to write a function that, given the dictionary and product name as an argument, will return the key for it and return "No" if the product name does not exist.

I used:

    return [key for key, value in product_dict.iteritems() if list(value)[0] == string]

And this returned the correct key when testing, but I don’t know how to make it return 'none' if the product name does not exist, and I'm not sure if this is the best way to do this. ''

I can use the built-in functions in python, any help is greatly appreciated!

+5
4

, .

- , . , . , len(), None, 0.

+2

, , , , . , 1, 0, .

([0]) . IndexError, . try/except None, IndexError .

+2

- ; . , .

( , , , ), , . , , , , , , , , :

# Restructure the dictionary
def invert_dictionary(input):
    out={}
    for bike_number in input.keys():                
        product_name, list_of_parts = input[bike_number]
        if not out.has_key(product_name):
            out[product_name]=[]        
        out[product_name].append((bike_number, list_of_parts))
    return out

new_dict = invert_dictionary(d)
# Returns a list of all bikes that are tagged "Racing Bike"
print new_dict['Racing Bike']

:

[('bike301', [('WH339', 2), ('TR302', 2), ('TU377', 2), ('FR301', 1), ('FB301', 1), ('BB301', 1), ('GS301', 1)])]

, . - , dict.

+1
source

In one line:

product_dict = {'bike301': ('Racing Bike', [('WH339', 2),('TR302', 2),
                                            ('TU377', 2),('FR301', 1),
                                            ('FB301', 1),('BB301', 1),
                                            ('GS301', 1)
                                            ]
                            ),
                'bike201': ('Mountain Bike', [('WH239', 2),('TR202', 2),
                                              ('TU277', 2),('FR201', 1),
                                              ('FB201', 1),('BB201', 1),
                                              ('GS201', 1)
                                              ]
                            ),
                'bike101': ('Road Bike', [('WH139', 2),('TR102', 2),
                                          ('TU177', 2),('FR101', 1),
                                          ('FB101', 1),('BB101', 1),
                                          ('GS101', 1)
                                          ]
                            )
                }


print dict( (string,k) for k,(name,li) in product_dict.iteritems() if name==string).get(string,None)

I do not see the benefits of having your vocabulary elements under the form number:(name,a_list)

I think it’s better to define:

product_dict2 = {('bike301','Racing Bike'):[('WH339', 2),('TR302', 2),
                                            ('TU377', 2),('FR301', 1),
                                            ('FB301', 1),('BB301', 1),
                                            ('GS301', 1)
                                            ],
                 ('bike201','Mountain Bike'):[('WH239', 2),('TR202', 2),
                                              ('TU277', 2),('FR201', 1),
                                              ('FB201', 1),('BB201', 1),
                                              ('GS201', 1)
                                              ],
                 ('bike101','Road Bike'):[('WH139', 2),('TR102', 2),
                                          ('TU177', 2),('FR101', 1),
                                          ('FB101', 1),('BB101', 1),
                                          ('GS101', 1)
                                          ]
                 }

Then for your need you should write:

print dict( (string,numb) for numb,name in product_dict2.iterkeys() if name==string).get(string,None)
0
source

All Articles