set

Is a set's “toArray” deterministic?

人走茶凉 提交于 2020-01-24 03:45:26
问题 Obviously, sets do not have any kind of ordering, so I cannot expect any specific ordering if I do String[] string = mySet.toArray(); However, I am faced with a use case where I don't care what ordering the string array is in, but I DO need it to be the case that if two sets are equal to each other, then: StringUtils.join(mySet.toArray(),','); will produce the same exact string for those sets, always, no matter how many times I run the program assuming I stick with the same code. Do I have

Python: How do sets work

醉酒当歌 提交于 2020-01-22 10:28:08
问题 I have a list of objects which I want to turn into a set. My objects contain a few fields that some of which are o.id and o.area . I want two objects to be equal if these two fields are the same. ie: o1==o2 if and only if o1.area==o2.area and o1.id==o2.id . I tried over-writing __eq__ and __cmp__ but I get the error: TypeError: unhashable instance . What should I over-write? 回答1: Define the __hash__ method to return a meaningful hash based on the id and area fields. E.g.: def __hash__(self):

How to have a set of structs in C++

早过忘川 提交于 2020-01-22 05:48:06
问题 I have a struct which has a unique key. I want to insert instances of these structs into a set. I know that to do this the < operator has to be overloaded so that set can make a comparison in order to do the insertion. The following does not work: #include <iostream> #include <set> using namespace std; struct foo { int key; }; bool operator<(const foo& lhs, const foo& rhs) { return lhs.key < rhs.key; } set<foo> bar; int main() { foo *test = new foo; test->key = 0; bar.insert(test); } 回答1:

Scala: Unable to set environment variable

百般思念 提交于 2020-01-21 15:19:38
问题 Friends, I'm trying to set the environment variable "asdf" in my Scala shell, as described here These are my commands: scala> import scala.sys.process.Process import scala.sys.process.Process scala> Process(Seq("bash", "-c", "echo $asdf"), None, "asdf" -> "Hello, world!").! Hello, world! res18: Int = 0 But when i try to read the environment variable back: scala> sys.env.get("asdf") res19: Option[String] = None The output says "None". How do i properly set my environment variable in the

Making a set from dictionary values

落爺英雄遲暮 提交于 2020-01-21 08:35:17
问题 I want to create a set from the values of an existing dict def function(dictionary): ... return set_of_values Say my dictionary looks like this: {'1': 'Monday', '2': 'Tuesday', '3': 'Monday'} I want a set returned that only contains the unique values, like this: {'Monday', 'Tuesday'} 回答1: For Python: set(d.values()) Equivalent on Python 2.7: set(d.viewvalues()) If you need a cross-compatible Python 2.7/3.x code: {d[k] for k in d} 回答2: Just another way to unique out: >>> my_dict = {'1':

How can I use an unordered_set with a custom struct?

无人久伴 提交于 2020-01-21 04:48:25
问题 I want to use an unordered_set with a custom struct . In my case, the custom struct represents a 2D point in an euclidean plane. I know that one should define a hash function and comparator operator and I have done so as you can see in my code below: struct Point { int X; int Y; Point() : X(0), Y(0) {}; Point(const int& x, const int& y) : X(x), Y(y) {}; Point(const IPoint& other){ X = other.X; Y = other.Y; }; Point& operator=(const Point& other) { X = other.X; Y = other.Y; return *this; };

Adding a tuple to a set does not work

时光毁灭记忆、已成空白 提交于 2020-01-21 04:28:50
问题 scala> val set = scala.collection.mutable.Set[(Int, Int)]() set: scala.collection.mutable.Set[(Int, Int)] = Set() scala> set += (3, 4) <console>:9: error: type mismatch; found : Int(3) required: (Int, Int) set += (3, 4) ^ scala> set += Tuple2(3, 4) res5: set.type = Set((3,4)) Adding (3, 4) does not work - why ? Normally, (3, 4) also represents a tuple with two elements. 回答1: The issue is that it exists in the Set trait a method +(elem1: A, elem2: A, elems: A+) and the compiler is confused by

Sinatra - response.set_cookie doesn't work

依然范特西╮ 提交于 2020-01-20 18:50:20
问题 I need to use a cookie for my Sinatra application. If I use the simpliest method is works: response.set_cookie('my_cookie', 'value_of_cookie') but I need some options such as domain and expire date so I try this: response.set_cookie("my_cookie", {:value => 'value_of_cookie', :domain => myDomain, :path => myPath, :expires => Date.new}) does not work. No cookie is made. I need this so much.... Please help... thanks! 回答1: The documentation on http://sinatra-book.gittr.com/#cookies says to use

add vs update in set operations in python

谁说胖子不能爱 提交于 2020-01-19 06:41:06
问题 What is the difference between add and update operations in python if i just want to add a single value to the set. a = set() a.update([1]) #works a.add(1) #works a.update([1,2])#works a.add([1,2])#fails Can someone explain why is this so. 回答1: set.add set.add adds an individual element to the set. So, >>> a = set() >>> a.add(1) >>> a set([1]) works, but it cannot work with an iterable, unless it is hashable. That is the reason why a.add([1, 2]) fails. >>> a.add([1, 2]) Traceback (most recent

How to sort a HashSet?

混江龙づ霸主 提交于 2020-01-19 04:42:06
问题 For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet ? 回答1: A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); ... List<?> sortedList = new ArrayList<>(yourHashSet); Collections.sort(sortedList); 回答2: Add all your objects