I'm having a difficult time learning python syntax. I've been doing some algorithms for a merge sort and I've run into a bit of an issue.  def arrMerge(a):     for i in range(1,len(a), *2):         for j in range(0,len(a)-1,2*i):             end2 = (2*i &lt; len(a) -j) ? 2*i : len(a) -j   this block in python any ideas on how I should go about executing it?
852
If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
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:
18119
I know there is a method for a Python list to return the first index of something:  &gt;&gt;&gt; l = [1, 2, 3] &gt;&gt;&gt; l.index(2) 1   Is there something like that for NumPy arrays?
9888
I am using the algorithmic package (or even algorithm2e is fine). Is it possible to use curly braces to group some lines in the algorithm so as to be able to put an explanation to the right?
12368
I need to use a priority queue in my Python code, and:   am looking for any fast implementations for priority queues optimally, I'd like the queue to be generic (i.e. work well for any object with a specified comparison operator).   Looking around for something efficient, I came upon , but:   I'm looking for something faster than heapq, which is implemented in native Python, so it's not fast. It looks good, but seems to be specified only for integers. I suppose it works with any objects that have comparison operators, but it doesn't specify what comparison operators it needs.  Update: Re comparison in heapq, I can either use a (priority, object) as Charlie Martin suggests, or just implement __cmp__ for my object.
17001
I'm wondering where I find the source to show how the operator ** is implemented in Python.  Can someone point me in the right direction?
76434
I have just started the MIT Introduction to Algorithms course through the material posted online. Along with the course I have also decided to learn/enhance my Ruby skills by coding the algorithms in it.   I am on the first algorithm given, which is Insertion sort, and I have the following code typed up but I am getting this error when I run it:     insertionsort.rb:5:in `>': comparison of Fixnum with nil failed (ArgumentError)   def  insertionsort(num) for j in 2..num.length     key = num[j]     i = j - 1     while i &gt; 0 and num[i] &gt; key         num[i+1] = num[i]         i = i - 1     end     num[i+1] = key end  puts num end  numbers = [23,34,46,87,12,1,66]  insertionsort(numbers)   I'm sure it is a fairly basic problem but I just can't grasp what it is at the moment. Any  help or tips would be very much appreciated.
35676
I am using the algorithmic package to typeset an algorithm. I would like to have connecting lines between the beginning and end of a loop or conditional like in the algorithm2e package. Is there an easy way to accomplish this, or am I better off switching to algorithm2e?
1704
This has always confused me. It seems like this would be nicer:  my_list = ["Hello", "world"] print(my_list.join("-")) # Produce: "Hello-world"   Than this:  my_list = ["Hello", "world"] print("-".join(my_list)) # Produce: "Hello-world"   Is there a specific reason it is like this?
90301
Formatting breaks if code follows a list:   List item List item  This is in a code block  This does not work as expected   However, it works if between the list and the code   List item List item   There is a line of normal text.  This is in a code block # This does work as expected   Edit  To make this clear: This is not about nesting code in a list but about code following a (finished) list not working as expected (by me). To put it differently: Why is a list not considered finished despite of following empty lines? What do I have to do?
49724
Take the following two lines of code:  for (int i = 0; i &lt; some_vector.size(); i++) {     //do stuff }   And this:  for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();     some_iterator++) {     //do stuff }   I'm told that the second way is preferred. Why exactly is this?
13251
Do you think there is a big difference in for...in and for loops? What kind of "for" do you prefer to use and why?  Let's say we have an array of associative arrays:  var myArray = [{'key': 'value'}, {'key': 'value1'}];   So we can iterate:  for (var i = 0; i &lt; myArray.length; i++)   And:  for (var i in myArray)   I don't see a big difference. Are there any performance issues?
13494
For a long period I thought the default behavior of the sort program was using ASCII order. However, when I input the following lines into sort without any arguments:  # @   I got:  @ #   But according to the ASCII table, # is 35 and @ is 64. Another example is:  A a   And the output is:  a A   Can anybody explain this? By the way, what is 'dictionary-order' when using sort -d?
70618
So I was writing up a simple binary tree in Python and came across [...]  I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however   &gt;&gt;&gt; a [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0] &gt;&gt;&gt; Keys(a)#With a+b [0, 1, 6, 8, 3, -4] &gt;&gt;&gt; Keys(a)#With [a,b] [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]] &gt;&gt;&gt; Keys(a)[1]#?? [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...], 8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]     Version using a+b  def Keys(x,y=[]):     if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#Though it seems I was using y=y[:]+, this actually outputs an ugly mess     return y   version using [a,b]  def Keys(x,y=[]):     if len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]     return y   So what exactly is [...]?
1022
The following code works as expected in both Python 2.5 and 3.0:  a, b, c = (1, 2, 3)  print(a, b, c)  def test():     print(a)     print(b)     print(c)    # (A)     #c+=1       # (B) test()   However, when I uncomment line (B), I get an UnboundLocalError: 'c' not assigned at line (A). The values of a and b are printed correctly. This has me completely baffled for two reasons:   Why is there a runtime error thrown at line (A) because of a later statement on line (B)? Why are variables a and b printed as expected, while c raises an error?   The only explanation I can come up with is that a local variable c is created by the assignment c+=1, which takes precedent over the "global" variable c even before the local variable is created. Of course, it doesn't make sense for a variable to "steal" scope before it exists.  Could someone please explain this behavior?
43330
In python, if I say  print 'h'   I get the letter h and a newline.  If I say   print 'h',   I get the letter h and no newline.  If I say  print 'h', print 'm',   I get the letter h, a space, and the letter m.  How can I prevent Python from printing the space?  The print statements are different iterations of the same loop so I can't just use the + operator.
19201
What is the best way to extend a dictionary with another one while avoiding the use of a for loop? For instance: &gt;&gt;&gt; a = { &quot;a&quot; : 1, &quot;b&quot; : 2 } &gt;&gt;&gt; b = { &quot;c&quot; : 3, &quot;d&quot; : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4}  Result: { &quot;a&quot; : 1, &quot;b&quot; : 2, &quot;c&quot; : 3, &quot;d&quot; : 4 }  Something like: a.extend(b)  # This does not work