set

Updating a set while iterating over its elements

回眸只為那壹抹淺笑 提交于 2019-12-20 15:29:31
问题 When I try to update a set while iterating over its elements, what should be its behavior? I tried it over various scenarios and it does not iterate over elements added after iteration is started and also the elements removed during iteration. If I remove and put back any element during iteration, that element is being considered. What's the exact behavior and how does it work? This prints all the permutations of a string: def permutations(s): ans = [] def helper(created, remaining): if len

Is it faster to union sets or check the whole list for a duplicate?

久未见 提交于 2019-12-20 15:14:16
问题 Sorry for the poorly worded title but I asked a question earlier about getting a unique list of items from two lists. People told me to make the list -> sets and then union. So now I'm wondering if it's faster to: While adding one item to a list, scan the whole list for duplicates. Make that one item a set and then union sets. I should probably just read up on sets in hindsight... In Python, by the way - sorry for not clarifying. 回答1: as you can see extending one list by another end then

ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

拟墨画扇 提交于 2019-12-20 11:52:57
问题 Java 8 introduced new way to obtain a concurrent Set implementation // Pre-Java-8 way to create a concurrent set Set<String> oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>()); // New method in Java 8 Set<String> newStyle = ConcurrentHashMap.newKeySet(); Is there any reason to prefer new method? Any advantages/disadvantages? 回答1: ConcurrentHashMap.newKeySet() should be somewhat more efficient as removes a single level of indirection. Collections.newSetFromMap(map) is mostly based

How do I add values to a Set inside a Map? [closed]

牧云@^-^@ 提交于 2019-12-20 10:44:17
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have this map Map<String, Set<Integer>> myMap; , now I need to interact with it, how do I do it? for example: Keys are: "apple", "orange", "grape", etc. Each set will contain random numbers: 1-9 I need to

Symmetric difference of two sets in Java

断了今生、忘了曾经 提交于 2019-12-20 10:23:22
问题 There are two TreeSet s in my app: set1 = {501,502,503,504} set2 = {502,503,504,505} I want to get the symmetric difference of these sets so that my output would be the set: set = {501,505} 回答1: You're after the symmetric difference. This is discussed in the Java tutorial. Set<Type> symmetricDiff = new HashSet<Type>(set1); symmetricDiff.addAll(set2); // symmetricDiff now contains the union Set<Type> tmp = new HashSet<Type>(set1); tmp.retainAll(set2); // tmp now contains the intersection

Python “set” with duplicate/repeated elements

这一生的挚爱 提交于 2019-12-20 10:21:47
问题 Is there a standard way to represent a "set" that can contain duplicate elements. As I understand it, a set has exactly one or zero of an element. I want functionality to have any number. I am currently using a dictionary with elements as keys, and quantity as values, but this seems wrong for many reasons. Motivation: I believe there are many applications for such a collection. For example, a survey of favourite colours could be represented by: survey = ['blue', 'red', 'blue', 'green'] Here,

Quickest algorithm for finding sets with high intersection

ⅰ亾dé卋堺 提交于 2019-12-20 09:37:07
问题 I have a large number of user IDs (integers), potentially millions. These users all belong to various groups (sets of integers), such that there are on the order of 10 million groups. To simplify my example and get to the essence of it, let's assume that all groups contain 20 user IDs. I want to find all pairs of integer sets that have an intersection of 15 or greater. Should I compare every pair of sets? (If I keep a data structure that maps userIDs to set membership, this would not be

List vs Queue vs Set of collections in Java

假装没事ソ 提交于 2019-12-20 08:41:01
问题 what is the difference among list, queue and set? 回答1: In brief: A list is an ordered list of objects, where the same object may well appear more than once. For example: [1, 7, 1, 3, 1, 1, 1, 5]. It makes sense to talk about the "third element" in a list. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list. A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at

How can I add items to an empty set in python

蓝咒 提交于 2019-12-20 08:35:53
问题 I have the following procedure: def myProc(invIndex, keyWord): D={} for i in range(len(keyWord)): if keyWord[i] in invIndex.keys(): D.update(invIndex[query[i]]) return D But I am getting the following error: Traceback (most recent call last): File "<stdin>", line 3, in <module> TypeError: cannot convert dictionary update sequence element #0 to a sequence I do not get any error if D contains elements. But I need D to be empty at the beginning. 回答1: D = {} is a dictionary not set. >>> d = {} >>

Python set Union and set Intersection operate differently?

我的未来我决定 提交于 2019-12-20 08:30:08
问题 I'm doing some set operations in Python, and I noticed something odd.. >> set([1,2,3]) | set([2,3,4]) set([1, 2, 3, 4]) >> set().union(*[[1,2,3], [2,3,4]]) set([1, 2, 3, 4]) That's good, expected behaviour - but with intersection: >> set([1,2,3]) & set([2,3,4]) set([2, 3]) >> set().intersection(*[[1,2,3], [2,3,4]]) set([]) Am I losing my mind here? Why isn't set.intersection() operating as I'd expect it to? How can I do the intersection of many sets as I did with union (assuming the [[1,2,3],