set-theory

efficiency of Python's itertools.product()

我的梦境 提交于 2021-02-04 18:39:45
问题 So I'm looking at different ways to compute the Cartesian product of n arrays, and I came across the rather elegant solution (here on SO) of using the following code: import itertools for array in itertools.product(*arrays): print array Looking at the python doc page (I'm using 2.7, btw) for itertools.product() , it says the code is equivalent to the following: def product(*args, **kwds): # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy # product(range(2), repeat=3) --> 000 001 010 011 100

SQL query for parent with specific values in child set

无人久伴 提交于 2021-01-28 12:52:39
问题 I am trying to return just the parent where the set of children contain multiple specific records. Given this table: Product State ------- ----- 111 AZ 111 CA 111 UT 222 AZ 222 WA 333 CA I want find the list of Product that have both a AZ child record and a CA child record, even if it also has other records. Which would return ... Product ------- 111 回答1: select product from table where state in ('az','ca') group by product having count(distinct(state)) = 2 select distinct product from table

SQL query for parent with specific values in child set

瘦欲@ 提交于 2021-01-28 12:41:45
问题 I am trying to return just the parent where the set of children contain multiple specific records. Given this table: Product State ------- ----- 111 AZ 111 CA 111 UT 222 AZ 222 WA 333 CA I want find the list of Product that have both a AZ child record and a CA child record, even if it also has other records. Which would return ... Product ------- 111 回答1: select product from table where state in ('az','ca') group by product having count(distinct(state)) = 2 select distinct product from table

Set theory data structure

陌路散爱 提交于 2020-01-24 13:59:06
问题 I come from a rather functional programming background and I'm not used to (efficient) C++ data structures. I need a data structure that keeps multiple elements like depicted in struct element . In the collection, the field id shall be unique. I want to perform a very fast set comparison like in set theory i.e. for example when comparing the sets {x1,x2,x3} and {x4,x5} I want to determine the intersection set {x5} (or {x2} which are equal in this case) and substract sets from other sets like

n={0,1,…,n-1} in C++

孤人 提交于 2020-01-06 06:55:42
问题 The formal definition (in set theory) of a natural number n is as follows: 0 is the empty set 1 = {0} n = {0,1,...,n-1} I think this would make some C++ code much simpler, if I was allowed to do this: for (int n : 10) cout << n << endl; and it printed numbers from 0 to 9. So I tried doing the following, which doesn't compile: #include <iostream> #include <boost/iterator/counting_iterator.hpp> boost::counting_iterator<int> begin(int t) { return boost::counting_iterator<int>(0); } boost:

Calling Redis zunionstore from Lua with variable KEYS

℡╲_俬逩灬. 提交于 2020-01-06 02:34:11
问题 I have a lua script that needs to call a zunionstore on a variable number of keys. I'm trying to execute the following code: local args = redis.call("zrange", "weight", 0, -1, "WITHSCORES") local r,w local count = 0 local cmd = ' ' for i=1,#args,2 do cmd = cmd .. args[i] .. ":weight " -- building up a list of zsets count = count + 1 end redis.call("zunionstore", "p2_test_set", count, cmd) The important lines being: cmd = cmd .. args[i] .. ":weight " Which builds the list of keys, and the

Why does L2E choke when I try to use an interface in my Where clause?

喜欢而已 提交于 2019-12-25 04:21:45
问题 I have a LINQ to Entities query (using EF 4) that has some pretty complicated set-based filtering going on. The code compiles just fine, but when I try to run it, I get the following error: Unable to create a constant value of type 'ITextEntity'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. Now for the code. I have an interface that looks like this: public interface ITextEntity { int ID { get; set; } string TextValue { get; set; } EntityCollection

Code to find unique elements among lists / sets

烈酒焚心 提交于 2019-12-24 14:19:51
问题 The above shaded area, according to Wolfram is supposed to represent: A XOR B XOR C XOR (A AND B AND C) How to translate this into python code? The code must closely correlate to the set operations as provided in the above expression, at least that is the preference. The code must be generic enough to handle more more than 3 lists as well. UPDATE: Seems like Wolfram is throwing up an erroneous venn diagram? Also, what we actually want is (A XOR B XOR C) - (A AND B AND C) and I am not able to

Finding the smallest solution set, if one exists (two multipliers)

与世无争的帅哥 提交于 2019-12-22 09:57:08
问题 Note: This is the two-multipliers variation of this problem Given a set A , consisting of floats between 0.0 and 1.0, find a smallest set B such that for each a in A , there is either a value where a == B[x] , or there is a pair of unique values where a == B[x] * B[y] . For example, given $ A = [0.125, 0.25, 0.5, 0.75, 0.9] A possible (but probably not smallest) solution for B is $ B = solve(A) $ print(B) [0.25, 0.5, 0.75, 0.9] This satisfies the initial problem, because A[0] == B[0] * B[1] ,

Python: frozensets comparison

Deadly 提交于 2019-12-20 04:25:10
问题 consider the following script: # multipleSmallFrozensets is a list of 7 frozensets of differenet number of string objects multipleSmallFrozensets = [ frozenset({'YHR007C', 'YHR042W'}), frozenset({'YPL274W'}), frozenset({'YCL064C'}), frozenset({'YBR166C'}), frozenset({'YEL041W', 'YJR049C'}), frozenset({'YGL142C'}), frozenset({'YJL134W', 'YKR053C'})] # singleFrozenset is a frozenset of 3410 string objects singleFrozenset = frozenset({'YIL140W','YLR268W','YLR357W','YJL155C','YHR067W', 'YAL008W',