Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
i have to implement the below line in one of my code:

for  p in sorted(segments.iterkeys()) and for k in
sorted(class_count.iterkeys()) and for j in sorted(pixel_count.iterkeys()):

Its giving me a syntax error which is obvious, but how can I make all three
for loop run simultaneously or any other way to do this simultaneous work
???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Chris Angelico
On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com wrote:
 i have to implement the below line in one of my code:

 for  p in sorted(segments.iterkeys()) and for k in
 sorted(class_count.iterkeys()) and for j in sorted(pixel_count.iterkeys()):

 Its giving me a syntax error which is obvious, but how can I make all three
 for loop run simultaneously or any other way to do this simultaneous work
 ???

Define simultaneously. Do the three dictionaries have the same number
of keys? If so, look up zip() or itertools.izip; if not, you may have
to more clearly define simultaneous.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
Yes Simultaneously means all three running at the same time, I looked up
zip just now, but will it not disturb my dictionaries ?
And yes the dictionaries have same number of keys.

thanks


On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.com wrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Gary Herron

On 04/22/2013 11:40 PM, inshu chauhan wrote:

i have to implement the below line in one of my code:

for  p in sorted(segments.iterkeys()) and for k in 
sorted(class_count.iterkeys()) and for j in 
sorted(pixel_count.iterkeys()):


Its giving me a syntax error which is obvious, but how can I make all 
three for loop run simultaneously or any other way to do 
this simultaneous work ???





Be clearer about the problem please.

Do you wish to produce a loop that:
  On pass 1, each of p,k, and t hold the first item of their respective 
lists, and
  on pass 2, each of p,k, and t hold the second item of their 
respective lists, and

  so on
until one (or all) lists run out?

If that is what you want, then check out the zip builtin function. But 
also consider this:  Do you care what happens if one list runs out 
before the others?




Or is it something else you want?  Perhaps nested loops?
  for  p in sorted(segments.iterkeys()):
  for k in sorted(class_count.iterkeys()):
  for j in sorted(pixel_count.iterkeys()):
 # This will be run with all possible combinations of p,k, 
and t.


Gary Herron




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
zip isn't doing the required


On Tue, Apr 23, 2013 at 12:28 PM, inshu chauhan insidesh...@gmail.comwrote:

 Yes Simultaneously means all three running at the same time, I looked up
 zip just now, but will it not disturb my dictionaries ?
 And yes the dictionaries have same number of keys.

 thanks


 On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.com wrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous
 work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
Thanks Gary.




 Be clearer about the problem please.

 Do you wish to produce a loop that:
   On pass 1, each of p,k, and t hold the first item of their respective
 lists, and
   on pass 2, each of p,k, and t hold the second item of their respective
 lists, and
   so on
 until one (or all) lists run out?


Yes this is excatly what I want each loop holds the first item on each
pass.


 If that is what you want, then check out the zip builtin function.  But
 also consider this:  Do you care what happens if one list runs out before
 the others?


Yes, but all dictionaries have same number of items.


 Or is it something else you want?  Perhaps nested loops?
   for  p in sorted(segments.iterkeys()):
   for k in sorted(class_count.iterkeys()):
   for j in sorted(pixel_count.iterkeys()):
  # This will be run with all possible combinations of p,k, and
 t


No, I know about nested loops but I dont want that because all the loops
have same number of items, inner loops will run out earlier.



 Gary Herron





 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
This statement is giving me the following error

Statement:
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):

Error:
Traceback (most recent call last):
  File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in
module
access_segments(segimage, data)
  File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in
access_segments
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):
TypeError: 'dictionary-keyiterator' object is not callable





On Tue, Apr 23, 2013 at 12:33 PM, inshu chauhan insidesh...@gmail.comwrote:

 zip isn't doing the required


 On Tue, Apr 23, 2013 at 12:28 PM, inshu chauhan insidesh...@gmail.comwrote:

 Yes Simultaneously means all three running at the same time, I looked up
 zip just now, but will it not disturb my dictionaries ?
 And yes the dictionaries have same number of keys.

 thanks


 On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.comwrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous
 work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Chris Angelico
On Tue, Apr 23, 2013 at 5:13 PM, inshu chauhan insidesh...@gmail.com wrote:
 This statement is giving me the following error

 Statement:
 for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
 pixel_count.iterkeys())):

You probably want to sort them separately. By the way, using
iterkeys() isn't going to do much for you, since you're sorting them;
you need to see all the keys to sort them.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Ulrich Eckhardt

Am 23.04.2013 09:13, schrieb inshu chauhan:

This statement is giving me the following error

Statement:
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):

Error:
Traceback (most recent call last):
   File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in
module
 access_segments(segimage, data)
   File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in
access_segments
 for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):
TypeError: 'dictionary-keyiterator' object is not callable


Which of the statements on that line causes the error? I guess asking 
yourself that question will lead you to the answer already! ;)


Any reason you quoted your own and several others' messages, am I 
missing some reference there?


Good luck!

Uli

--
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Duncan Booth
inshu chauhan insidesh...@gmail.com wrote:

 This statement is giving me the following error
 
 Statement:
 for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
 pixel_count.iterkeys())):
 
 Error:
 Traceback (most recent call last):
   File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in
module
 access_segments(segimage, data)
   File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in
 access_segments
 for p, k, j in zip(sorted(segments.iterkeys(),
 class_count.iterkeys(), 
 pixel_count.iterkeys())):
 TypeError: 'dictionary-keyiterator' object is not callable
 

The second argument to `sorted()` is a comparison or key function, if you 
want to sort all three key lists you need to sort them separately. Try:

for p, k, j in zip(sorted(segments),
   sorted(class_count),
   sorted(pixel_count)):

also you don't need to call the `iterkeys()` method as you need them all to 
sort and just treating the dict as a sequence will do the right thing.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread Dave Angel

On 04/23/2013 02:58 AM, inshu chauhan wrote:

Yes Simultaneously means all three running at the same time, I looked up
zip just now, but will it not disturb my dictionaries ?
And yes the dictionaries have same number of keys.



More crucially, do all the dictionaries have the *same* keys?  If so, 
then all the zip logic is unnecessary, as the p, k, and j values will 
always be identical.


If they have the same keys, then do something like:

for key in sorted(segments):
val1 = segments[key]
val2 = class_count[key]
val3 = pixel_count[key]
... do something with those values

If any of the keys is missing in one of the other dicts, you'll get an 
exception.  You might also want to do a check of the size of the 3 
dicts, just in case.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list