combinations

Combining lists in python

风流意气都作罢 提交于 2019-12-19 10:44:06
问题 I am trying to combine 2 lists and want to form combinations. a = ['ibm','dell'] b = ['strength','weekness'] I want to form combinations like ['ibm strength','ibm weekness','dell strength','dell weakness'] . I tried to use zip or concatenated the lists. I also used itertools but it doesn't give me desired output. Please help. a = ['ibm','dell'] b = ['strength','weekness'] c = a + b itertools.combinations(c,2) for a in a: for b in b: print a +b 回答1: You're looking for product(). Try this:

Calculate Nth multiset combination (with repetition) based only on index

谁说我不能喝 提交于 2019-12-19 10:19:35
问题 How can i calculate the Nth combo based only on it's index. There should be (n+k-1)!/(k!(n-1)!) combinations with repetitions. with n=2, k=5 you get: 0|{0,0,0,0,0} 1|{0,0,0,0,1} 2|{0,0,0,1,1} 3|{0,0,1,1,1} 4|{0,1,1,1,1} 5|{1,1,1,1,1} So black_magic_function(3) should produce {0,0,1,1,1}. This will be going into a GPU shader, so i want each work-group/thread to be able to figure out their subset of permutations without having to store the sequence globally. with n=3, k=5 you get: i=0, {0,0,0,0

Postgresql enforce unique two-way combination of columns

北城余情 提交于 2019-12-19 08:16:08
问题 I'm trying to create a table that would enforce a unique combination of two columns of the same type - in both directions. E.g. this would be illegal: col1 col2 1 2 2 1 I have come up with this, but it doesn't work: database=> \d+ friend; Table "public.friend" Column | Type | Modifiers | Storage | Stats target | Description --------------+--------------------------+-----------+----------+--------------+------------- user_id_from | text | not null | extended | | user_id_to | text | not null |

No of numbers less than a given number with no repeating digits

半城伤御伤魂 提交于 2019-12-19 03:42:47
问题 How can we find the number of numbers less than a given number with no repeating digits in it? For example the number of such numbers less than 100 is 90. (11, 22, 33,44, 55,66,77,88,99 have repeating digits so are excluded). Similarly for less than 1000, digits like 101, 110, 122, 202 etc have to be excluded. 回答1: Here is a way to make it quicker. Notice that there is a correlation between the number of digits in the max number and the solution (number of numbers which I will call NON ) 100

php generate all combinations from given array

混江龙づ霸主 提交于 2019-12-19 03:39:09
问题 What is the simplest way to convert this PHP array $a = array('A' => array(1, 2), 'B' => array(3, 4), 'C' => array(5)); into this: $result = array( array('A' => 1, 'B' => 3, 'C' => 5), array('A' => 1, 'B' => 4, 'C' => 5), array('A' => 2, 'B' => 3, 'C' => 5), array('A' => 2, 'B' => 4, 'C' => 5), ); $a may have many different keys I don't know during development time. So I need to generate all combinations in given array. UPDATE: I have to generate URLs based in incoming array. So I don't know

C++ Newbie needs helps for printing combinations of integers

孤人 提交于 2019-12-18 17:56:24
问题 Suppose I am given: A range of integers iRange (i.e. from 1 up to iRange ) and A desired number of combinations I want to find the number of all possible combinations and print out all these combinations. For example: Given : iRange = 5 and n = 3 Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10 combinations, and the output is: 123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345 Another example: Given : iRange = 4 and n = 2 Then the number of

Output each combination of an array of numbers with javascript

↘锁芯ラ 提交于 2019-12-18 16:55:33
问题 I have several numbers in an array var numArr = [1, 3, 5, 9]; I want to cycle through that array and multiply every unique 3 number combination as follows: 1 * 3 * 5 = 1 * 3 * 9 = 1 * 5 * 9 = 3 * 5 * 9 = Then return an array of all the calculations var ansArr = [15,27,45,135]; Anyone have an elegant solution? Thanks in advance. 回答1: A general-purpose algorithm for generating combinations is as follows: function combinations(numArr, choose, callback) { var n = numArr.length; var c = []; var

count unique combinations of values

半城伤御伤魂 提交于 2019-12-18 16:51:31
问题 My dataframe looks like this: ID | value 1 | value 2 | value 3 | value 4 1 | M | D | F | A 2 | F | M | G | B 3 | M | D | F | A 4 | L | D | E | B I want to get something like this. value 1 | value 2 | value 3 | value 4| Number of combinations M | D | F | A | 2 F | M | G | B | 1 L | D | E | B | 1 e.g. to count the number of unique combinations of the columns value 1 - value 4. 回答1: count in plyr package will do that task. > df ID value.1 value.2 value.3 value.4 1 1 M D F A 2 2 F M G B 3 3 M D F

What is the fastest algorithm to computer all permutations of a binary number with same hamming weight?

对着背影说爱祢 提交于 2019-12-18 16:45:18
问题 I want an algorithm to compute all permutations of a fixed size binary number with a given hamming weight. For example, if the hamming weight is 2 and the binary size is 4 then there are these outputs: 0011 0110 0101 1100 1010 1001 The number of such combinations is computed as C(n,r) in this example C(4,2) which is 6. Note that you can solve this just by increasing a number from 0 to 2^n and see if the count is OK. However, it is not a fast solution. I am considering solving the problem

Finding common elements in list in python

本小妞迷上赌 提交于 2019-12-18 09:38:28
问题 Finding common elements in list in python? Imagine if i have a list like follows [[a,b],[a,c],[b,c],[c,d],[e,f],[f,g]] My output must be [a,b,c,d] [e,f,g] How do i do it? What i tried is like this for i in range(0,len(fin3)): for j in range(i+1,len(fin3)): grop = [] grop = list(set(fin3[i]) & set(fin3[j])) if len(grop)>0: grop2 = [] grop2.append(link[i]) grop2.append(link[j]) grop3.append(grop2) Thanks in advance... 回答1: I think you want something like: data = [[1, 2], [2, 3], [4, 5]] output