I am working on a python script, which requires reading values from a list in for loop and performing some operations on it. currently it looks like this:  for i in someList:     # do some operation here   Now, this particular operation takes hours to complete, so what i want to be able to do is use multi-threading/sub-processes and process multiple items in parallel. So is there a way that I can run the for loop like:  for i,j,k in someList:     #do some operation here   i know this syntax can be used to iterate over multiple lists simultaneously, but I want to be able to use multiple items of the same list. What i want from the loop is that the value of i,j and k update in the following manner:  i = someList[1], j = someList[2], k = someList[3]   in the next iteration:  i = someList[4], j = someList[5], k = someList[6]
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
29528
So, I have 10 CPU core and 20 data to process. I want to process the data in parallel but I am afraid if I just process 20 at once it will make some problem. So, I want to process 10 data 2 times. Is there any command to do this?  Add info:  The data are in file format. It is quite huge, per file can reach 10GB. In my expreience if I launch more than 10 process, the PC will become really slow and even lag. So I am limiting the process to be only 10 which is equal to the number of cores. As for my RAM, I believe the software which process the file will not load everything at once so the RAM usage is quite low. That is why I just need to parallel the process for every 10 data. For now, I generate 10 shell script which execute parallel and each shell script contains sequential command.
8317
If I have sequence of sequences (maybe a list of tuples) I can use itertools.chain() to flatten it. But sometimes I feel like I would rather write it as a comprehension. I just can't figure out how to do it. Here's a very construed case:  Let's say I want to swap the elements of every pair in a sequence. I use a string as a sequence here:  &gt;&gt;&gt; from itertools import chain &gt;&gt;&gt; seq = '012345' &gt;&gt;&gt; swapped_pairs = zip(seq[1::2], seq[::2]) &gt;&gt;&gt; swapped_pairs [('1', '0'), ('3', '2'), ('5', '4')] &gt;&gt;&gt; "".join(chain(*swapped_pairs)) '103254'   I use zip on the even and odd slices of the sequence to swap the pairs. But I end up with a list of tuples that now need to be flattened. So I use chain(). Is there a way I could express it with a comprehension instead?  If you want to post your own solution to the basic problem of swapping elements of the pairs, go ahead, I'll up-vote anything that teaches me something new. But I will only mark as accepted an answer that is targeted on my question, even if the answer is "No, you can't.".
95401
If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?  If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?  sorry if this is a dumb question, but it is something I have always wondered about Python on multi-processor/core machines.    same thing would apply to any other language implementation that has a GIL.
82458
In order to optimize my script, I would need some help to perform a rotation around the cursor (see )  With low-level python, that means, without bpy.ops.* operations.  I have to admit that I didn't find a good overview on how to make rotation with python without bpy.ops. Do I have to perform matrix operations for a simple rotation around an axis Z located at the cursor position?
27093
I occasionally see the list slice syntax used in Python code like this:  newList = oldList[:]   Surely this is just the same as:  newList = oldList   Or am I missing something?
38476
I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB.   FileA:  [jaypal:~/Temp] cat filea this is File A line1 this is File A line2 this is File A line3   FileB:  [jaypal:~/Temp] cat fileb this is File B line1 this is File B line2 this is File B line3   Current Sample Script:  [jaypal:~/Temp] cat read.sh  #!/bin/bash while read lineA     do echo $lineA      while read lineB         do echo $lineB          done &lt; fileb done &lt; filea   Execution:  [jaypal:~/Temp] ./read.sh  this is File A line1 this is File B line1 this is File B line2 this is File B line3 this is File A line2 this is File B line1 this is File B line2 this is File B line3 this is File A line3 this is File B line1 this is File B line2 this is File B line3   Problem and desired output:  This loops over FileB completely for each line in FileA. I tried using continue, break, exit but none of them are meant for achieving the output I am looking for. I would like the script to read just one line from File A and then one line from FileB and exit the loop and continue with second line of File A and second line of File B. Something similar to the following script -  [jaypal:~/Temp] cat read1.sh  #!/bin/bash count=1 while read lineA     do echo $lineA          lineB=`sed -n "$count"p fileb`         echo $lineB         count=`expr $count + 1` done &lt; filea  [jaypal:~/Temp] ./read1.sh  this is File A line1 this is File B line1 this is File A line2 this is File B line2 this is File A line3 this is File B line3   Is this possible to achieve with while loop?
108829
What is the difference between multiprocessor programming and multicore programming? preferably show examples in python how to write a small program for multiprogramming &amp; multicore programming
1360
I want to create a list of lambda objects from a list of constants in Python; for instance:  listOfNumbers = [1,2,3,4,5] square = lambda x: x * x listOfLambdas = [lambda: square(i) for i in listOfNumbers]   This will create a list of lambda objects, however, when I run them:  for f in listOfLambdas:     print f(),   I would expect that it would print  1 4 9 16 25   Instead, it prints:  25 25 25 25 25   It seems as though the lambdas have all been given the wrong parameter. Have I done something wrong, and is there a way to fix it? I'm in Python 2.4 I think.  EDIT: a bit more of trying things and such came up with this:  listOfLambdas = [] for num in listOfNumbers:     action = lambda: square(num)     listOfLambdas.append(action)     print action()   Prints the expected squares from 1 to 25, but then using the earlier print statement:  for f in listOfLambdas:     print f(),   still gives me all 25s. How did the existing lambda objects change between those two print calls?  Related question:
740
I have a list of arbitrary length, and I need to split it up into equal size chunks and operate on it. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.  I was wondering if anyone had a good solution to this for lists of any length, e.g. using generators.  I was looking for something useful in itertools but I couldn't find anything obviously useful. Might've missed it, though.  Related question:
111448
I have two iterators, a list and an itertools.count object (i.e. an infinite value generator). I would like to merge these two into a resulting iterator that will alternate yield values between the two:  &gt;&gt;&gt; import itertools &gt;&gt;&gt; c = itertools.count(1) &gt;&gt;&gt; items = ['foo', 'bar'] &gt;&gt;&gt; merged = imerge(items, c)  # the mythical "imerge" &gt;&gt;&gt; merged.next() 'foo' &gt;&gt;&gt; merged.next() 1 &gt;&gt;&gt; merged.next() 'bar' &gt;&gt;&gt; merged.next() 2 &gt;&gt;&gt; merged.next() Traceback (most recent call last):     ... StopIteration   What is the simplest, most concise way to do this?
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
74382
i have:  for i in range(2,n):     if(something):        do something     else:        do something else        i = 2 **restart the loop   But that doesn't seem to work. Is there a way to restart that loop?   Thanks
62799
I have 8 python scripts and I need to lunch them at once. For one script, I used   blender --background --python script1.py   for the eight scripts, I tried this command but it does not work.  blender --background --python *.py
78638
How can I iterate over each file in a directory using a for loop?  And how could I tell if a certain entry is a directory or if it's just a file?
34869
How can I iterate, loop, process, or otherwise do something, with each element from a comma separated list of things?  This question       , and the are already lots of very good answers everywhere. However I think it would also be valuable to have a single reference point where all such alternative techniques are explained in detail, with their advantages and disadvantages discussed.  All of plain-TeX, LaTeX, LaTeX3, LuaTeX, Context, etoolbox, pgffor, etc. answers are invited. Please consider explaining, for example:   How does the command/method deal with whitespace around items? How does the command/method deal with empty/missing items? (e.g. a,b,,c) How does the command/method deal with a trailing comma? Can the command/method work of a list stored on a macro? Is each iteration evaluated globally or on a local group? (i.e., if a command is defined during an iteration, does the command survive the loop?) Do items get expanded or somehow mangled before being processed? What about lists where items are key=value or some other kind of pairs? Does the method work for lists separated with something other than a comma? Any other thing I should know before deciding to use this method?
63486
Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume processing.  In general, one would opt for using Lists (List) due to their flexibility in size. On top of that, msdn documentation claims Lists use an array internally and should perform just as fast (a quick look with Reflector confirms this). Neverless, there is some overhead involved.  Did anyone actually measure this? would iterating 6M times through a list take the same time as an array would?