looping in python

1118
6
02-10-2011 07:33 AM
MikePowell
Deactivated User
I believe this is a simple question but I can't seem to wrap my brain around it. I am using EasyGUI multichoice box. I have 32 items that I am allowing the user to pick from. When they click ok, I need to run those items selected. I have set up each of the 32 items as a function, so basically, I just need to call each one from the selection of the user. I have it to that point. The problem I am having is getting it to call just the ones selected and to only run them once. I think it is a for loop that I need but can't figure out how to set it up correctly. I know it is not a if elseif statement because that will only run the first selection. Any help would be appreciated
Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus
done so you can see the logic
>>> a_list = [1,2,3,4,5,6,7,8,9,10]
>>> needed = [1,5,8]
>>> for aval in needed:
...  if aval in a_list:
...      print "found ", aval
...      
found  1
found  5
found  8
>>> 

you can also use dictionaries etc
0 Kudos
MikePowell
Deactivated User
>>> a_list = [1,2,3,4,5,6,7,8,9,10]
>>> needed = [1,5,8]
>>> for aval in needed:
...  if aval in a_list:
...      print "found ", aval
...     
found  1
found  5
found  8
>>>

Yes, thank you. But how do I call just the functions that are in aval, so I can just run those
0 Kudos
LukeBadgerow
Deactivated User
I come up with this based on the comments I've read so far
list = [f1...f32]
needed = [f5(params), f6(params), f12(params)]
for func in needed:
     if func in list:
          func(params)



if you've got a def f5(params) someplace else in your code you might be able to get away without passing the params into your list but you're also running a bit of a risk of not being able to make the call because the way the list is built.

You might want to look at Lambda expressions because it's sounding like you're trying to put together a jump table.  I personally don't mess with them, little complicated for me to read through quickly (Mark Twain theory of programming --  $0.10 words are better than $3 words) same reason I don't use tertiary statements in Java/C#

Hope this helps.
0 Kudos
MikePowell
Deactivated User
list = [f1...f32]
needed = [f5(params), f6(params), f12(params)]
for func in needed:
     if func in list:
          func(params)

Thanks but my question, than, to you is, What is "params"? You have "list" and "needed" is just items from "list" so it can't be different from what is in "list", right? Am I not understanding it correctly (very possible).

Would I have to set each item in list to equal the corresponding function but than I still do not know how to have it run just the functions in "needed"
0 Kudos
DanPatterson_Retired
MVP Emeritus
I am not sure what you need, but the demo below may be of some help or will further confuse the issue.  It assumes that you have a bunch of functions (defs) that you want to call based upon some index number (or other dictionary structure), it doesn't do much but it demos what can be done.
def func1(inputs=None):
  x = sum(inputs)
  return "sum some numbers: ", x
'''
more functions
'''
def func5(inputs=None):
  x_sq = 0
  for x in inputs:
    x_sq += x**2
  return "sum of squares: ", x_sq
'''
more functions
'''
def func8(inputs=None):
  return "hello from 8: ", inputs

'''
more functions
'''
a_list = [1,2,3,4,5,6,7,8,9,10]
inputs = "test inputs"
a_dict = {1:[func1([1,2,3]) ],
          5:[func5([1,2,3])],
          8:[func8("inputs to 8")]}
needed = [1,5,8]
for akey in needed:
  if akey in a_list:
    action = a_dict[akey]
    print "\naction: ", action
0 Kudos
MikePowell
Deactivated User
Thanks for info. I don't understand Dictionaries too much but I will take a look at this and see what I can learn.
0 Kudos