Well this is kind of an elementary question, but here it goes:  Consider the following code:  listA = ['a','b','c'] listB = listA listB.pop(0) print listB print listA   The output comes as:  ['b','c'] ['b','c']   However, shouldn't the output be:  ['b','c'] ['a','b','c']   What exactly is happening here? And how could I get the expected output? Thanks in advance :)
410
I needed to create a list of lists in Python, so I typed the following: my_list = [[1] * 4] * 3  The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]    Then I changed one of the innermost values: my_list[0][0] = 5  Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]    which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?
3114
I'd like to do it in Python. What I'd like to do in this example in C: #include &lt;stdio.h&gt;  int main() {     int i;     for (i=0; i&lt;10; i++) printf(&quot;.&quot;);     return 0; }  Output: ..........  In Python: &gt;&gt;&gt; for i in range(10): print('.') . . . . . . . . . . &gt;&gt;&gt; print('.', '.', '.', '.', '.', '.', '.', '.', '.', '.') . . . . . . . . . .  In Python print will add a \n or space, how can I avoid that? Now, it's just an example, don't tell me I can first build a string then print it. I'd like to know how to &quot;append&quot; strings to stdout.
64302
I frequently find myself writing code like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; foreach (int i in list) {     Console.Write("{0}\t", i.ToString()); } Console.WriteLine();   Better would be something like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; Console.WriteLine("{0}\t", list);   I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?
3472
Is it possible to split an itemize list into several columns? (I'm sure it is, but I couldn't find a solution around here)  And additionally: Is it possible to automatically split a list into multiple columns if it reaches a certain item length?  so i want to display  item1 item2 item3   instead of   item1   item2   item3   while this should still happen  item1   item4 item2   item5 item3   item6
2050
Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?
21860
I'm trying to debug a Python script by placing a print("foo=" + foo) statement in my code.  But where does the output appear?     It doesn't appear on the Python Console, and it doesn't appear on the area that appears when you drag the lower edge of the infobar downwards (what is the name for this area? My script errors show up in red here.)
27564
The code below generates:    What I expect is:    Why does the \foreach seem to treat the entire list as one value? How do I fix it?  \documentclass{article} \usepackage{xparse} \usepackage{xstring} \usepackage{etoolbox} \usepackage{expl3} \usepackage{tikz}  \begin{document}  \ExplSyntaxOn  \DeclareDocumentCommand{\separate}{m} {   \StrLeft{#1}{1}[\head]   \StrBehind[1]{#1}{\head}[\tail]   \IfInteger{\tail}{\head,\separate{\tail}}{\head} }  \DeclareDocumentCommand{\map}{m} {   \def\list{#1}   \foreach \i in \list   {     [\i]   } }  \ExplSyntaxOff  \def\arg{3669887}  \arg  \separate{\arg}  \map{\separate{\arg}}  \end{document}
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:
14937
Sometimes it seems natural to have a default parameter which is an empty list. Yet . If for example, I have a function: def my_func(working_list=[]):     working_list.append(&quot;a&quot;)     print(working_list)  The first time it is called the default will work, but calls after that will update the existing list (with one &quot;a&quot; each call) and print the updated version. So, what is the pythonic way to get the behavior I desire (a fresh list on each call)?
100626
For example {a..c}{1..3} expands to a1 a2 a3 b1 b2 b3 c1 c2 c3.   If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3, is there an analogous way to do that? What's the simplest way?
2664
In python, is there a difference between calling clear() and assigning {} to a dictionary? If yes, what is it? Example:d = {"stuff":"things"} d.clear()   #this way d = {}      #vs this way
96075
Here is a quick question, how to use tex to print this ' symbol? An example is shown below:  data 'a list = Nil | Cons (e, 'a list)   I tried to use ' or `, but they all looks bad.. I am sorry but I am not a native English speaker, so I don't even know the name of '..   Could anyone help me on this simple problem? thank you
11923
I'm trying to split a string up into words and punctuation, adding the punctuation to the list produced by the split.  For instance:  &gt;&gt;&gt; c = "help, me" &gt;&gt;&gt; print c.split() ['help,', 'me']   What I really want the list to look like is:  ['help', ',', 'me']   So, I want the string split at whitespace with the punctuation split from the words.  I've tried to parse the string first and then run the split:  &gt;&gt;&gt; for character in c: ...     if character in ".,;!?": ...             outputCharacter = " %s" % character ...     else: ...             outputCharacter = character ...     separatedPunctuation += outputCharacter &gt;&gt;&gt; print separatedPunctuation help , me &gt;&gt;&gt; print separatedPunctuation.split() ['help', ',', 'me']   This produces the result I want, but is painfully slow on large files.  Is there a way to do this more efficiently?
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?
23996
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:  &gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa'   Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated.
42368
I have a python list, say l  l = [1,5,8]   I want to write a sql query to get the data for all the elements of the list, say  select name from students where id = |IN THE LIST l|   How do I accomplish this?
57835
says:  If the index number is @ or *, all members of an array are referenced.  When I do this: LIST=(1 2 3) for i in &quot;${LIST[@]}&quot;; do   echo &quot;example.$i&quot; done  it gives the desired result: example.1 example.2 example.3  But when I use ${LIST[*]}, I get example.1 2 3  instead. Why? Edit: when using printf, @ and * actually do give the same results.