Request: Efficient procedure to manually loop between two lists in python -
i feel silly asking have been struggling couple of days on how improve code , cannot discern obvious way improve actual design rather inefficient , ugly despite supposed simplicity...
i need move across couple of lists using key events.
for simplicity's sake let's first list “shops” , second 1 “products”
shops = [shopa,shopb,shopc,shopcd,shope]
products = [oranges, milk, water, chocolate, rice]
let's keyboards keys “1” , “2” want move forward , backward respectively along “shops” list. on other hand, want use keys “3” , “4” move forward , backward respectively on “products” list.
now, method have consists in couple of counters: “shopcounter” , “productscounter”. incidentally, each time move along “shops” list set “productscounter” 0 can start @ checking @ first item (in example oranges :)
my problem lies in location/design of counters. have 4 sections in code: 1 each key instruction want , input (next/previous shop; next/previous product).
since python starts lists @ 0 obvious reason place counters @ end of each code section. however, if must press twice each key backward/forward key if previous command forward/backward. since preserves increased counter previous command
if put counters @ beginning of each section (which makes each section more... easier understand...) have set “if” check make sure code not overcome first items on list. following design, must check additional if check make sure not try find item beyond list length...
any advice on right way handle these kind of operations?? welcome.
edit:
this code represents example have provided structure change. works, think there should way in more logical way.
import matplotlib.pyplot plt def countermanager(key, shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter): if key == "1" or key == "2": productscounter = 0 if shopscounter == 0 , shopsstartercheck == true: shopsstartercheck = false print "eo" elif key == "1" : shopscounter = shopscounter + 1 elif key == "2" : shopscounter = shopscounter - 1 if shopscounter <= 0: shopscounter = 0 if shopscounter >= shopnumber - 1: shopscounter = shopnumber - 1 productsstartercheck = true print "--shop: " + str(shopscounter) if key == "3" or key == "4": if productscounter == 0 , productsstartercheck == true: productsstartercheck = false elif key == "3" : productscounter = productscounter + 1 elif key == "4" : productscounter = productscounter - 1 if productscounter <= 0: productscounter = 0 if productscounter >= productsnumber - 1: productscounter = productsnumber - 1 print "--product: " + str(productscounter) return shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter def key_manager(event): global shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter if event.key == '1' or event.key == '2': shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter = countermanager(event.key, shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter) print shops[shopscounter] if event.key == '3' or event.key == '4': shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter = countermanager(event.key, shopnumber, productsnumber, shopsstartercheck, productsstartercheck, shopscounter, productscounter) print products[productscounter] shops = ["shopa","shopb","shopc","shopd","shope"] products = ["oranges", "milk", "water", "chocolate", "rice"] shopnumber, productsnumber = len(shops), len(products) shopsstartercheck,productsstartercheck = true,true shopscounter,productscounter, = 0,0 fig1=plt.figure(figsize=(10,5)) axis1 = fig1.add_subplot(111) fig_connection = {} fig_connection['cid'] = plt.gcf().canvas.mpl_connect('key_press_event',key_manager) #create figure connect event plt.show()
Comments
Post a Comment