Say I have a list   my_input_list = [22,33,56,1]   and from a variable target_len I want to make (exclusive, sorted) sublists of that length  target_len = 2 #my_output_list_not_sorted = [[22,33][22,56][22,1][33,56][33,1][56,1]] my_output_list = [[22,33][22,56][1,22][33,56][1,33][1,56]]   or   target_len = 3 #my_output_list_not_sorted = [[[22,33,56][22,33,1][33,56,1]] my_output_list = [[22,33,56][1,22,33][1,33,56]]   Is there a neat way to do this?  I'm OK to sort my_input_list first if needed.  Thanks!
2272
I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers.   I've found  (by Googling) that apparently does what I'm looking for, but I found the code fairly opaque and am wary of using it. Plus I have a feeling there must be a more elegant solution.  The only thing that occurs to me would be to just loop through the decimal integers 1–32768 and convert those to binary, and use the binary representation as a filter to pick out the appropriate numbers.   Does anyone know of a better way? Using map(), maybe?
102822
I know about , but in my case I need something that implements List, and not Set. So is there an implementation out there, in the API or elsewhere?  It shouldn't be hard to implement myself, but I figured why not ask people here first?
60659
I have a situation where I want to define a command that takes a variable number of arguments, where the number of arguments is known programmatically via a \count, and process the parameters in some way (say as if they are a list).  As an example, say I'd like to output the parameters as a comma-separated list.  \newcommand{\makecsv}[N]{#1, #2, ..., #N}   The code that I've come up with to do this kind of operation (in a generic-ish way) essentially takes a command, \csv and expands it recursively N times. \csv needs to know how to continue the recursion, and has some state that I'd like to thread through the recursion (rather than using \global).  \documentclass{report} \usepackage{etoolbox}  \makeatletter \newcommand{\ifzero}[3]{%   % #1: count   % #2: state for #3   % #3: macro to expand to   % - should take at least 2 parameters   % - ##1: count threaded through   % - ##2: macro state threaded through   \ifnum\c &gt; 0     \def\tmp@f##1##2##3{##1{##2}{##3}}%     \advance#1 -1%   \else     \def\tmp@f##1##2##3{)}% note closeparen here (could be param)   \fi   \tmp@f{#3}{#1}{#2}% } \makeatother  \newcommand{\csv}[3]{   % #1: count   % #2: separator state   % #3: string to concat   %    #2#3\ifzero{#1}{, }{\csv}% }  \newcommand{\makecsv}[1]{%   \ifzero{#1}{}{\csv}% }  \makeatletter \newcommand{\decl}[3]{%   % #1: decl id   % #2: decl symbol   % #3: # params   \csgdef{decl@#1}{#2}%   \global\expandafter\newcount\csname decl@#1@nparams\endcsname%   \csuse{decl@#1@nparams} #3\relax% }  \newcommand{\usedecl}[1]{%   \newcount\c   \c \the\csuse{decl@#1@nparams}   \csuse{decl@#1}(\makecsv{\c}% } \makeatother  % declare some interface routines \decl{foo}{FOO}{3} \decl{bar}{BAR}{4}  \begin{document} \usedecl{foo}{p1}{p2}{p3}\par \usedecl{bar}{p1}{p2}{p3}{p4}\par \end{document}   Is this a reasonable thing to do in 2e, or is there some sort of standard approach to this that is normally used?  Edit 1  It seems like my original MWE wasn't adequate to describe why someone might want this. I've updated the MWE with a use case. \decl allows authors to declaratively define a C-style function, and \usedecl allows the author to generate a use of the function, with its parameters bound to specific arguments.  This is similar enough to what I'm doing that it should help motivate the example.
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:
52342
I need to randomly 'sort' a list of integers (0-1999) in the most efficient way possible.  Any ideas?  Currently, I am doing something like this:  bool[] bIndexSet = new bool[iItemCount];  for (int iCurIndex = 0; iCurIndex &lt; iItemCount; iCurIndex++) {     int iSwapIndex = random.Next(iItemCount);     if (!bIndexSet[iSwapIndex] &amp;&amp; iSwapIndex != iCurIndex)     {         int iTemp = values[iSwapIndex];         values[iSwapIndex] = values[iCurIndex];         values[iCurIndex] = values[iSwapIndex];         bIndexSet[iCurIndex] = true;         bIndexSet[iSwapIndex] = true;     } }
18727
This question is somewhat of a follow up to an answer to a previous question ().  I would like to create a list environment (similar to itemize and enumerate) such that the output of the list environment is a comma-separated list.  For example, the following input:  \begin{commalist}     \item Item A     \item Item B     \item Item C \end{commalist}   Should produce the following output:  Item A, Item B and Item C.   Additionally, it would be nice to be able to specify the use of a comma instead of the final and (so that the output is instead Item A, Item B, Item C..
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?
29483
What is the best way of creating an alphabetically sorted list in Python?
23463
I need a quick algorithm to select 5 random elements from a generic list. For example, I'd like to get 5 random elements from a List&lt;string&gt;.
9898
Code like this often happens: l = [] while foo:     # baz     l.append(bar)     # qux  This is really slow if you're about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements. In Java, you can create an ArrayList with an initial capacity. If you have some idea how big your list will be, this will be a lot more efficient. I understand that code like this can often be refactored into a list comprehension. If the for/while loop is very complicated, though, this is unfeasible. Is there an equivalent for us Python programmers?
87293
The following code uses the etoolbox package to convert a list a,b,c into an internal list (called \mylist) and then typesets that with colons: a:b:c:.  However, when I load the etextools package (by uncommenting the second line), I get no list output.   \documentclass{minimal} \usepackage{etoolbox} %\usepackage{etextools} \begin{document} A list: \def\mylist{} \forcsvlist{\listadd\mylist}{a,b,c} \def\do#1{#1:} \dolistloop{\mylist} \end{document}   I suspect it is is a bug since etextools claims to depend on etoolbox.  Is this a bug or am I doing something wrong?  In either case, how can I use \dolistloop with  etextools (which I want for goodies like \expandnext)?  Notes   \show\mylist produces the same output a|b|c| in both cases, so the problem is with \dolistloop which expands to \forlistloop\do The \forlistloop command becomes substantially more complicated when etextools is used.  might be relevant (but not obviously).
70845
Suppose that, for a real application, I have ended up with a sorted list A = {$a_1, a_2, ..., a_{|A|}$} of elements of a certain kind (say, Type-A), and another sorted list B = {$b_1, b_2, ..., b_{|B|}$} of elements of a different kind (Type-B), such that Type-A elements are only comparable with Type-A elements, and likewise for Type-B.  At this point I seek to count the following: in how many ways can I merge both lists together, in such a way that the relative ordering of Type-A and Type-B elements, respectively, is preserved? (i.e. that if $P_M(x)$ represents the position of an element of A or B in the merged list, then $P_M(a_i)&lt;P_M(a_j)$ and $P_M(b_i)&lt;P_M(b_j)$ for all $i&lt;j$)  I've tried to figure this out constructively by starting with an empty merged list and inserting elements of A or B one at a time, counting in how many ways each insertion can be done, but since this depends on the placement of previous elements of the same type, I've had little luck so far. I also tried explicitly counting all possibilities for different (small) lengths of A and B, but I've been unable to extract any potential general principle in this way.
49084
How do I remove an element from a list by index in Python?  I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.
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?
44787
If you are creating a 1d array, you can implement it as a List, or else use the 'array' module in the standard library.  I have always used Lists for 1d arrays.  What is the reason or circumstance where I would want to use the array module instead?  Is it for performance and memory optimization, or am I missing something obvious?
7281
I have a list of dictionaries and want each item to be sorted by a specific value. Take into consideration the list: [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]  When sorted by name, it should become: [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
65725
I have a list of items that looks like this:  A B C A A B D E A   Now I want to count the number of occurrences of each item. The result should look like this:  A 4 B 2 C 1 D 1 E 1   How can I do that? It is important to note that this should be flexible. That means if I add item F to the list, that item should also be considered in the result.