set

Order of insertion in sets (when parsing {}) [duplicate]

丶灬走出姿态 提交于 2019-12-18 13:09:20
问题 This question already has an answer here : Dict/Set Parsing Order Consistency (1 answer) Closed 2 years ago . Someone asked here why when putting 1 and True in a set only 1 is kept. This is of course because 1==True . But in which cases 1 is kept and in which cases True is kept? Let's see: passing a list to build the set instead of using the set notation: >>> set([True,1]) {True} >>> set([1,True]) {1} seems logical: set iterates on the inner list, and doesn't add the second element because it

append set to another set

人盡茶涼 提交于 2019-12-18 12:45:46
问题 Is there a better way of appending a set to another set than iterating through each element ? i have : set<string> foo ; set<string> bar ; ..... for (set<string>::const_iterator p = foo.begin( );p != foo.end( ); ++p) bar.insert(*p); Is there a more efficient way to do this ? 回答1: You can insert a range: bar.insert(foo.begin(), foo.end()); 回答2: It is not a more efficient but less code. bar.insert(foo.begin(), foo.end()); Or take the union which deals efficiently with duplicates. (if applicable

Creating sets of similar elements in a 2D array

☆樱花仙子☆ 提交于 2019-12-18 11:34:42
问题 I am trying to solve a problem that is based on a 2D array. This array contains different kinds of elements (from a total of 3 possible kinds). Lets assume the kind as X, Y, Z. The array appears to be something like this. Note that it would always be completely filled. The diagram is for illustration. 7 | | | | | | | 6 | | | | | | | 5 | | | | | | | 4 | |X|Z|Y|X| | 3 | |Y|X|Y|Y|X| 2 |Y|Y|X|Z|Z|X| 1 |X|X|Y| |X|X| 0 | | | |Z| | | 0 1 2 3 4 5 I am trying to create sets of elements that are placed

set object is not JSON serializable [duplicate]

試著忘記壹切 提交于 2019-12-18 11:25:18
问题 This question already has answers here : How to JSON serialize sets? (6 answers) Closed 5 years ago . When I try to run the following code: import json d = {'testing': {1, 2, 3}} json_string = json.dumps(d) I get the following exception: Traceback (most recent call last): File "json_test.py", line 4, in <module> json_string = json.dumps(d) File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps return _default_encoder.encode(obj) File "/usr/lib/python2.7/json/encoder.py", line 207, in

set object is not JSON serializable [duplicate]

冷暖自知 提交于 2019-12-18 11:24:06
问题 This question already has answers here : How to JSON serialize sets? (6 answers) Closed 5 years ago . When I try to run the following code: import json d = {'testing': {1, 2, 3}} json_string = json.dumps(d) I get the following exception: Traceback (most recent call last): File "json_test.py", line 4, in <module> json_string = json.dumps(d) File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps return _default_encoder.encode(obj) File "/usr/lib/python2.7/json/encoder.py", line 207, in

Quickly checking if set is superset of stored sets

 ̄綄美尐妖づ 提交于 2019-12-18 11:09:12
问题 The problem I am given N arrays of C booleans. I want to organize these into a datastructure that allows me to do the following operation as fast as possible: Given a new array, return true if this array is a "superset" of any of the stored arrays. With superset I mean this: A is a superset of B if A[i] is true for every i where B[i] is true. If B[i] is false, then A[i] can be anything. Or, in terms of sets instead of arrays: Store N sets (each with C possible elements) into a datastructure

lambda versus list comprehension performance

隐身守侯 提交于 2019-12-18 10:49:21
问题 I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test: import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time() N=[x for x in S for y in T if x==y] time2 = time.time() print 'time diff [x for x in S for y in T if x==y]=', time2-time1 #print N # # time1 = time.time() N=filter(lambda x:x in S,T) time2 =

intersection of tuples in a list - python

时光怂恿深爱的人放手 提交于 2019-12-18 08:55:00
问题 I have a list of tuples like this : all_tuples=[(92, 242),(355, 403),(355, 436),(355, 489),(403, 436),(436, 489),(515, 517),(517, 859),(634, 775),(701, 859),(775, 859)] and I need to take the intersection of all tuples and union them. The desired result = [{92, 242},{355, 403,436,489},{515, 517,859,701,775,634}] That is the intersected tuples are union iteratively. I tried to convert the tuples to sets and then take the intersection but did not work. Any idea? 回答1: This is network problem ,

Python: What's the difference between set.difference and set.difference_update?

丶灬走出姿态 提交于 2019-12-18 08:30:36
问题 s.difference(t) returns a new set with no elements in t . s.difference_update(t) returns an updated set with no elements in t . What's the difference between these two set methods? Because the difference_update updates set s, what precautions should be taken to avoid receiving a result of None from this method? In terms of speed, shouldn't set.difference_update be faster since you're only removing elements from set s instead of creating a new set like in set.difference()? 回答1: Q. What's the

How can I improve this design that forces me to declare a member function const and declare variables mutable?

£可爱£侵袭症+ 提交于 2019-12-18 08:17:11
问题 For some reason I am iterating over elements of a class in an std::set and would like to slightly modify the keys, knowing that the order will be unchanged. Iterators on std::set are const_iterators because if the key is modified, it might result in a bad order and therefore in set corruption. However I know for sure that my operations won't change the order of my elements in the set. For the moment, here is my solution: class Foo { public: Foo(int a, int b): a_(a),b_(b) {} ~Foo(){} bool