Jump to content

for loop in one line


pelos

Recommended Posts

i am trying to put a code into a button that will print the values of some parameters, i was able to make a list with

t = [x for x in hou.pwd().parms() if x.name().startswith("blend")];

then i can print the list of the parameters that  i want with 

print t;

 

 

but if i want to do something to each one is when starts to break:

t = [x for x in hou.pwd().parms() if x.name().startswith("blend")]; print t; for i in t:   print i.eval();

 

 

 

the funny thing is if i print all directly with out doing the filter list does work =(

for i in hou.pwd().parms():   print i.eval();

 

 

any cool tip that might work?

 

thanks.

Link to comment
Share on other sites

You should be able to do something like this:

for i in (x for x in hou.pwd().parms() if x.name().startswith("blend")): print i.eval()

It removes one of the loops, but you also have to do it as a generator expression, rather than a list comprehension, thus the () instead of the [].

  • Like 1
Link to comment
Share on other sites

Unfortunately I'm not too sure about the intricacies about Python and single line statements.  It's also no a Houdini specific thing.  If you take the giant line from up there that doesn't work in Houdini, it doesn't work in Python either.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...