multiset

Python list intersection with non unique items

↘锁芯ラ 提交于 2019-11-28 09:54:10
I have two strings and I would like to have the intersection on them including duplicate items: str_a = "aabbcc" str_b = "aabd" list(set(str_a) & set(str_b)) >> "ab" I would like to have it return: >> "aab" Any ideas? ninjagecko Multisets are implemented in python 2.7 or later as (mutable) Counter objects. You can perform many of the same operations as you can for sets, such as union, intersection, difference (though counts can become negative), etc.: from collections import Counter as mset Solution: (mset("aabbcc") & mset("aabd")).elements() More details: >>> intersection = mset("aabbcc") &

Test if set is a subset, considering the number (multiplicity) of each element in the set

牧云@^-^@ 提交于 2019-11-28 04:43:47
问题 I know I can test if set1 is a subset of set2 with: {'a','b','c'} <= {'a','b','c','d','e'} # True But the following is also True: {'a','a','b','c'} <= {'a','b','c','d','e'} # True How do I have it consider the number of times an element in the set occurs so that: {'a','b','c'} <= {'a','b','c','d','e'} # True {'a','a','b','c'} <= {'a','b','c','d','e'} # False since 'a' is in set1 twice but set2 only once {'a','a','b','c'} <= {'a','a','b','c','d','e'} # True because both sets have two 'a'

Moving elements out of an associative container

ε祈祈猫儿з 提交于 2019-11-27 18:28:10
问题 Just for fun, I have implemented the simplest sorting algorithm imaginable: template<typename Iterator> void treesort(Iterator begin, Iterator end) { typedef typename std::iterator_traits<Iterator>::value_type element_type; // copy data into the tree std::multiset<element_type> tree(begin, end); // copy data out of the tree std::copy(tree.begin(), tree.end(), begin); } It's only about 20 times slower than std::sort for my test data :) Next, I wanted to improve the performance with move

Adding counters deletes keys

寵の児 提交于 2019-11-27 15:30:55
See below, why does the implementation of += blow away a key in my original counter? >>> c = Counter({'a': 0, 'b': 0, 'c': 0}) >>> c.items() [('a', 0), ('c', 0), ('b', 0)] >>> c += Counter('abba') >>> c.items() [('a', 2), ('b', 2)] I think that's impolite to say the least, there is quite a difference between "X was counted 0 times" and "we aren't even counting Xs". It seems like collections.Counter is not a counter at all, it's more like a multiset. But counters are a subclass of dict and we're allowed to construct them with zero or negative values: Counter(a=0, b=-1) . If it's actually a "bag

Generating all combinations with repetition using MATLAB

南笙酒味 提交于 2019-11-27 08:57:31
How do I create all k-combinations with repetitions of a given set (also called k-multicombinations or multisubsets ) using MATLAB? This is similar to the cartesian product, but two rows that only differ by their sorting should be considered the same (e.g. the vectors [1,1,2]=~=[1,2,1] are considered to be the same), so generating the cartesian product and then applying unique(sort(cartesianProduct,2),'rows') should yield the same results. Example: The call nmultichoosek(1:n,k) should generate the following matrix: nmultichoosek(1:3,3) ans = 1 1 1 1 1 2 1 1 3 1 2 2 1 2 3 1 3 3 2 2 2 2 2 3 2 3

Is there an algorithm to generate all unique circular permutations of a multiset?

a 夏天 提交于 2019-11-27 03:22:40
问题 I encountered this problem when doing some enthusiastic programming. The problem can be expressed as follows: For a multiset A, let P(A) denote the set of all possible permutations of A. P(A) is naturally divided into disjoint subsets that are equivalence classes, with the equivalence relation being "can be related by circular shifts." Enumerate all these equivalence classes by generating exactly one member from each of them. For example, consider the multiset {0, 1, 1, 2}. The permutations

Simplest way to iterate through a Multiset in the order of element frequency?

被刻印的时光 ゝ 提交于 2019-11-27 01:48:43
Consider this example which prints out some device type stats. ("DeviceType" is an enum with a dozenish values.) Multiset<DeviceType> histogram = getDeviceStats(); for (DeviceType type : histogram.elementSet()) { System.out.println(type + ": " + histogram.count(type)); } What's the simplest, most elegant way to print the distinct elements in the order of their frequency (most common type first)? With a quick look at the Multiset interface, there's no ready-made method for this, and none of Guava's Multiset implementations ( HashMultiset , TreeMultiset , etc) seem to automatically keep elements

Adding counters deletes keys

江枫思渺然 提交于 2019-11-26 18:32:21
问题 See below, why does the implementation of += blow away a key in my original counter? >>> c = Counter({'a': 0, 'b': 0, 'c': 0}) >>> c.items() [('a', 0), ('c', 0), ('b', 0)] >>> c += Counter('abba') >>> c.items() [('a', 2), ('b', 2)] I think that's impolite to say the least, there is quite a difference between "X was counted 0 times" and "we aren't even counting Xs". It seems like collections.Counter is not a counter at all, it's more like a multiset. But counters are a subclass of dict and we

R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

折月煮酒 提交于 2019-11-26 12:59:42
In this thread, I am trying to include all the commonly asked questions and their answers here. I hope this will be useful for someone. General question : How to generate sequences of r objects from n objects? combination vs permutation. with replacement vs without replacement. distinct items vs non-distinct items (multisets). There are in total 2^3=8 questions of this type. [Update] Josh O'Brien suggests that the 8 questions are related to twelvefold way . Indeed, the "distinct" questions are included in twelvefold way, while the "non-distinct" questions are not included. Anyway, it is

Generating all combinations with repetition using MATLAB

 ̄綄美尐妖づ 提交于 2019-11-26 12:46:34
问题 How do I create all k-combinations with repetitions of a given set (also called k-multicombinations or multisubsets ) using MATLAB? This is similar to the cartesian product, but two rows that only differ by their sorting should be considered the same (e.g. the vectors [1,1,2]=~=[1,2,1] are considered to be the same), so generating the cartesian product and then applying unique(sort(cartesianProduct,2),\'rows\') should yield the same results. Example: The call nmultichoosek(1:n,k) should