Is there a function that generates all the permutations of a list of lists, while keeping their orders? For example: func([[1, 2, 3], [10, 20, 30]])  produces: [[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30], [3, 10], [3, 20], [3, 30]]
1477
How can I get the Cartesian product (every possible combination of values) from a group of lists?  Input:   somelists = [    [1, 2, 3],    ['a', 'b'],    [4, 5] ]   Desired output:  [(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5) ...]
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?
12777
I am writing a program in Python, and I realized that a problem I need to solve requires me, given a set S with n elements (|S|=n), to test a function on all possible subsets of a certain order m (i.e. with m number of elements). To use the answer to produce a partial solution, and then try again with the next order m=m+1, until m=n.  I am on my way to write a solution of the form:  def findsubsets(S, m):     subsets = set([])     ...     return subsets   But knowing Python I expected a solution to be already there.  What is the best way to accomplish this?
66541
I've noticed confusing definitions about permutation and substitution, preventing me from understanding the difference.  A permutation changes the order of distinct elements of a set, but this can be written as a function changing one element by another, exactly as a substitution.  So, how exactly can we distinguish between these two notions?  This question is interesting since we have these two notions in block ciphers, in which S-boxes perform substitution. The problem is that this table is used to replace one element by another, exactly like a permutation.
59822
What is the number of automorphisms (including identity) for permutation group $S_3$ on 3 letters?  I believe the answer for this is 6. As we can write the group elements as below   (a)(b)(C) (ab)(c) (ac)(b) (bc)(a) (abc) (bac)   Can we generalize that for any $S_n$ onto $n$ the number of automorphisms will be $n!$ Also I cannot find this anywhere in my text, can we have a permutation $S_n$ onto $m$ where $n\neq m$? (for eg: is it possible to have a permutation group say $S_5$ on say 2 elements?)
16494
When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
87966
Let $x_1, \ldots ,x_n$ be distinct points in $\Bbb{R}^d$. Prove that there is a permutation $\sigma$ of $[n]$ such that the $\angle x_{\sigma(i−1)}x_{\sigma(i)}x_{\sigma(i+1)} &lt; \pi/2$ for any $i = 2,\ldots, n−1$.  So I have got this question, and I am not able to understand what &quot;a permutation $\sigma$ of $[n]$&quot; mean? Can someone explain to me this question and provide some hints to approach it? P.S. I have edited out some parts as it was a bit irrelevant.
37793
Given a list of people:  [   1,   2,   3,   4,   5,   6 ]   The goal is to generate some number of lists of pairs.  [   [     [1,2],     [3,4],     [5,6]   ],   [     [2,3],     [4,5],     [6,1]   ],   [     [3,1],     [5,2],     [4,6]   ],   [     [2,6],     [3,5],     [4,1]   ],   [     [4,2],     [5,1],     [3,6]   ] ]   Each list of pairs represents the schedule for that week. Each pair represents the people who should meet with that week.  In the example above, in the first week, persons 1 &amp; 2, 3 &amp; 4, 5 &amp; 6 will meet. In the second week, persons 2 &amp; 3, 4 &amp; 5, 6 &amp; 1 will meet and so on until everyone has meet with each other.  After some experimenting, it appears that for a list of n people, if n is even, there will be n - 1 lists of pairs. If n is odd, there will be n lists of pairs and one person will have a bye week where they don't meet with anyone.  Is there an algorithm to generate this list of pairs given a list of people?
63961
Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem came with a natural recursive solution. Now it happens that, even for relatively small input, the sequences are several thousands, thus I would prefer to use my algorithm as a generator instead of using it to fill a list with all the sequences.  Here is an example. Suppose we want to compute all the permutations of a string with a recursive function. The following naive algorithm takes an extra argument 'storage' and appends a permutation to it whenever it finds one:  def getPermutations(string, storage, prefix=""):    if len(string) == 1:       storage.append(prefix + string)   # &lt;-----    else:       for i in range(len(string)):          getPermutations(string[:i]+string[i+1:], storage, prefix+string[i])  storage = [] getPermutations("abcd", storage) for permutation in storage: print permutation   (Please don't care about inefficiency, this is only an example.)  Now I want to turn my function into a generator, i.e. to yield a permutation instead of appending it to the storage list:  def getPermutations(string, prefix=""):    if len(string) == 1:       yield prefix + string             # &lt;-----    else:       for i in range(len(string)):          getPermutations(string[:i]+string[i+1:], prefix+string[i])  for permutation in getPermutations("abcd"):    print permutation   This code does not work (the function behaves like an empty generator).  Am I missing something? Is there a way to turn the above recursive algorithm into a generator without replacing it with an iterative one?
34678
I've a List of this type List> that contains this  List&lt;int&gt; A = new List&lt;int&gt; {1, 2, 3, 4, 5}; List&lt;int&gt; B = new List&lt;int&gt; {0, 1}; List&lt;int&gt; C = new List&lt;int&gt; {6}; List&lt;int&gt; X = new List&lt;int&gt; {....,....};   I want to have all combinations like this  1-0-6 1-1-6 2-0-6 2-1-6 3-0-6   and so on.  According to you is This possibile to resolve using Linq?
107703
How many permutations of $\{1, \ldots, n\}$ exist such that none of them contain $(i, i+1)$ (as a sequence of two consecutive entries) for $i \in \left\{1,...,(n-1)\right\}$?  First thing that comes to my mind is to find all that have $(i, i+1)$, then subtract that from all permutations. But then we can have $(i, i+1, i+2)$ which we subtracted twice, once in $(i, i+1)$ and once in $(i+1, i+2)$. And so on for $3$ and more. How do I calculate this?
117148
In how many ways can the letters of word $PERMUTATIONS$ be arranged if there are always $4$ letters between $P$ and $S$?   Now there are $12$ blank spaces, which we have to fill by the letters of the given word.  That is :-  [][][][][][][][][][][][]  it is given that two places are always filled with P and S such that there are 4 blank spaces between them.  {[P][][][][][S]}[][][][][][]  $Therefore$,  the number of arrangements that can be made such that there are 4 letter between $P$ and $S$ is $P(10,4)$  $p(10, 4) = \frac{10!}{(10 - 4)!} =\frac{10!}{6!} = 10 \times 9 \times 8 \times 7 = 5040 \tag 1$  Also {[P][][][][][S]} can be arranged in different ways with respect to {[P][][][][][S]}[][][][][][].  $Therefore$ the number arrangements will be $p(7, 7)$  $p(7, 7,) = 7! = 5040 \tag 2$  $Therefore$ the the total number of arrangements for the given question will be   $5040 \times 5040 \text{ (results of 1 and 2)}\\ = 25401600 \tag3$  but there are two T's in the word permutation  $Therefore$ we divde the result of 3 by 2 to get the real answer.  That is , $$\frac{25401600}{2} = 12700800$$  So I am getting $12700800$ as the answer but it is wrong the given answer is $25401600$. How ?    Thanks and sorry for the formatting.
28573
I want to make an enumerated list in the following format:  [1] foo foo [2] foo foo ...    How I can do that using enumerated lists? I have tried the following:  \begin{enumerate}[\left[ 1 \right]]   but it does not work.
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:
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?
117273
I declared random list using TikZ package and can get items randomly. But at same time I need to get all items in sequential order. How can I do it? Is it possible print \mylist items declared in noncomma variat?  \documentclass[a4paper,14pt]{extreport} \usepackage{tikz}   \def\mylist{{one}{two}{three}{four}{five}}   \pgfmathdeclarerandomlist{mynum}{\mylist} %Define the list   \begin{document} \pgfmathrandomitem{\mynum}{mynum} \pgfmathresult~\mynum\\  \pgfmathrandomitem{\mynum}{mynum} \pgfmathresult~\mynum\\  \pgfmathrandomitem{\mynum}{mynum} \pgfmathresult~\mynum\\  \pgfmathrandomitem{\mynum}{mynum} \pgfmathresult~\mynum\\  \pgfmathrandomitem{\mynum}{mynum} \pgfmathresult~\mynum\\ \end{document}
99159
I didn't understand the permutation and of course, I got this question wrong.     Compute the inverse of the following permutation:   $$ \begin{pmatrix}  1&amp;2&amp;3&amp;4&amp;5&amp;6\\ 2&amp;5&amp;4&amp;1&amp;6&amp;3 \end{pmatrix} $$   Could you please help me with this question and provide an explanation?  Furthermore, is it possible to compute the direct permutation, not the inverse one? If yes, how?  Thank you in advance!