hi based on this code:

def load_products(filename):
	def get_components(parts):
	    """parts is a list of parts and their associated amounts, each separated into groups, split by (',')"""
	    """need to take out each id, take out each integer, an associate them, by putting them in a pair -> return in big list"""
	    """turn 'parts' into a string"""
	    partss = "".join(parts)
	    """ -> Reference: 'http://mail.python.org/pipermail/tutor/2005-May/038160.html'"""
	    info = partss.split(':')
	    parts_list = []
	    for l in info:
	    return parts_list

	filename = ('products.txt')
	o = open(filename, 'U')
	product_dict = {}
	for line in o:
		split_line = line.split(',')
		global ID
                """ID is referenced elsewhere"""
		ID = split_line[0]
		global name
		"""name is referenced elsewhere"""
		name = split_line[1].strip()
		global parts
		"""parts is referenced elsewhere"""
		parts = split_line[2:][:-2]
		product_dict[ID] = name, parts
	return product_dict]

i need to write a function that will allow a user to search by ID, and get name, and a function that will search by name and return an id does anyone know how i can do this?

7 Years
Discussion Span
Last Post by woooee

if ID in product_dict:
for starters.

values_list = product_dict.values()
will give a list of all the values. This http://www.daniweb.com/code/snippet806.html is an example, but the easiest way is to reverse the original dictionary, unless it is huge. That is, have one dictionary that is key-->value to look up the key, and a second dictionary that is name-->key to look up the value (unless this is homework and you have to do it a certain way). If you have a lot of records, consider using SQL, as it easily does a lookup by field. Also, instead of the globals, consider using a dictionary with "ID", "name", and "parts" as the keys to store the info, and pass the dictionary to the function. Speaking of passing variables, you pass a filename to the function but don't use it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.