set

Does JavaScript have an implementation of a set data structure?

↘锁芯ラ 提交于 2019-12-17 06:11:40
问题 I'm looking for a decent implementation of a set data structure in JavaScript. It should be able to support elements that are plain JavaScript objects. So far I only found Closure Library's structs.Set, but I don't like the fact that it modifies my data. 回答1: You could build a simple wrapper around the keys of a hash table provided by my jshashtable. I have one knocking around somewhere that I will dig out later. UPDATE I have completed and tested an implementation of HashSet and uploaded it

Empty set literal?

帅比萌擦擦* 提交于 2019-12-17 05:33:27
问题 [] = empty list () = empty tuple {} = empty dict Is there a similar notation for an empty set ? Or do I have to write set() ? 回答1: No, there's no literal syntax for the empty set. You have to write set() . 回答2: Just to extend the accepted answer: From version 2.7 and 3.1 python has got set literal {} in form of usage {1,2,3} , but {} itself still used for empty dict. Python 2.7 (first line is invalid in Python <2.7) >>> {1,2,3}.__class__ <type 'set'> >>> {}.__class__ <type 'dict'> Python 3.x

data.table with two string columns of set elements, extract unique rows with each row unsorted

你。 提交于 2019-12-17 04:37:26
问题 Suppose I have a data.table like this: Table: V1 V2 A B C D C A B A D C I want each row to be regarded as a set, which means that B A and A B are the same. So after the process, I want to get: V1 V2 A B C D C A In order to do that, I have to first sort the table row-by-row and then use unique to remove the duplicates. The sorting process is quite slow if I have millions of rows. So is there an easy way to remove the duplicates without sorting? 回答1: For just two columns you can use the

Getting an element from a Set

扶醉桌前 提交于 2019-12-17 04:12:12
问题 Why doesn't Set provide an operation to get an element that equals another element? Set<Foo> set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains an element equal to bar , so why can't I get that element? :( To clarify, the equals method is overridden, but it only checks one of the fields, not all. So two Foo objects that are considered equal can actually have different values, that's why I

What makes sets faster than lists?

别来无恙 提交于 2019-12-17 03:55:29
问题 The python wiki says: "Membership testing with sets and dictionaries is much faster, O(1), than searching sequences, O(n). When testing "a in b", b should be a set or dictionary instead of a list or tuple." I've been using sets in place of lists whenever speed is important in my code, but lately I've been wondering why sets are so much faster than lists. Could anyone explain, or point me to a source that would explain, what exactly is going on behind the scenes in python to make sets faster?

Efficiently compute Intersection of two Sets in Java?

自古美人都是妖i 提交于 2019-12-17 03:32:56
问题 What is the most efficient way to find the size of the intersection of two non-sparse Sets in Java? This is an operation I will be calling on large sets a very large number of times, so optimisation is important. I cannot modify the original sets. I have looked at Apache Commons CollectionUtils.intersection which appears to be quite slow. My current approach is to take the smaller of the two sets, clone it, and then call .retainAll on the larger of the two sets. public static int

How to calculate the intersection of two sets? [duplicate]

陌路散爱 提交于 2019-12-17 02:33:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Efficiently finding the intersection of a variable number of sets of strings Say, have two Hashset, how to calculate the intersection of them? Set<String> s1 = new HashSet<String>(); Set<String> s2 = new HashSet<String>(); S1 INT S2 ? 回答1: Use the retainAll() method of Set: Set<String> s1; Set<String> s2; s1.retainAll(s2); // s1 now contains only elements in both sets If you want to preserve the sets, create a

C# Set collection?

情到浓时终转凉″ 提交于 2019-12-17 02:26:57
问题 Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way. 回答1: Try HashSet: The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order... The capacity of a HashSet(Of T) object is the number of elements that the

Java Set retain order?

折月煮酒 提交于 2019-12-17 02:12:48
问题 Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the method need to be changed to return something other than a Set? 回答1: The Set interface does not provide any ordering guarantees. Its sub-interface SortedSet represents a set that is sorted according to some criterion. In Java 6, there are two standard containers that implement SortedSet . They are

In Python, when to use a Dictionary, List or Set?

不羁的心 提交于 2019-12-17 02:01:36
问题 When should I use a dictionary, list or set? Are there scenarios that are more suited for each data type? 回答1: A list keeps order, dict and set don't: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course;-). dict associates with each key a value, while list and set just contain values: very different use cases, obviously. set requires items to be hashable, list doesn't: if you have non-hashable items, therefore, you cannot