set

Best way to find the intersection of multiple sets?

血红的双手。 提交于 2019-12-16 22:21:41
问题 I have a list of sets: setlist = [s1,s2,s3...] I want s1 ∩ s2 ∩ s3 ... I can write a function to do it by performing a series of pairwise s1.intersection(s2) , etc. Is there a recommended, better, or built-in way? 回答1: From Python version 2.6 on you can use multiple arguments to set.intersection(), like u = set.intersection(s1, s2, s3) If the sets are in a list, this translates to: u = set.intersection(*setlist) where *a_list is list expansion Note that set.intersection is not a static method

hashCode与equals的区别与联系

旧街凉风 提交于 2019-12-14 23:22:19
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、equals方法的作用 1、 默认情况(没有覆盖equals方法)下equals方法都是调用Object类的equals方法,而Object的equals方法主要用于判断对象的内存地址引用是不是同一个地址(是不是同一个对象)。 2 、要是类中覆盖了equals方法,那么就要根据具体的代码来确定equals方法的作用了,覆盖后一般都是通过对象的内容是否相等来判断对象是否相等。 没有覆盖equals方法代码如下: [java] view plain copy //学生类 public class Student { private int age; private String name; public Student() { } public Student( int age, String name) { super (); this .age = age; this .name = name; } public int getAge() { return age; } public String getName() { return name; } public void setAge( int age) { this .age = age; } public void setName(String

Python新手学习基础之数据结构-对数据结构的认知

戏子无情 提交于 2019-12-14 12:15:29
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 什么是数据结构? 数据结构是指:相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成。 举个列子来理解这个数据结构: 数据可以比作是书本, 数据结构相当于书架,书存放在书架上,要拿书,我们就得到书架上面去取。为了更快的拿到想要的书,可以把书按照某个方式来排列。比如将书架分成N层,(卡通书在第一层,文学书在第二层......)不同种类的书放在书架的不同层里。 这就是对数据结构通俗的一种理解。 Python的数据结构分类 Python中的数据结构可以统称为容器(container,即存放数据的容器),它有三种类型:序列(列表和元组),集合(Set)和映射(如字典): 列表List 例如: list1 = ['p', 'y', 't', 'h', 'o', 'n'] 元组Tuple 例如: tuple1 = ('p', 'y', 't', 'h', 'o', 'n') 集合Set 例如: set1={'p', 'y', 't', 'h', 'o', 'n'} 字典Dict 例如: dict1 ={1:'p', 2:'y', 3:'t', 4:'h', 5:'o', 6:'n'} 这些数据结构有什么区别和作用呢?我们接下来的文章中结合一些实用的数据结构操作的方法,逐一介绍它们

get difference between 3 lists

萝らか妹 提交于 2019-12-14 04:17:26
问题 I am working on differences of lists. >>a = [1, 2, 3] >>b = [2, 4, 5] >>c = [3, 2, 6] Symmetric difference between 2 sets can be done using: >>z = set(a).symmetric_difference(set(b)) >>print z >>set([1, 3, 4, 5]) How to get difference between 3 sets? For difference of 3 sets, expected output is : expected output : set([1, 3, 4, 5, 6]) 回答1: Just subtract the intersection from the union: In [1]: a = set([1, 2, 3]) In [2]: b = set([2, 4, 5]) In [3]: c = set([3, 2, 6]) In [4]: (a | b | c) - (a &

How get List from Set and Comparator

*爱你&永不变心* 提交于 2019-12-14 03:43:29
问题 What is the "good" (and why ?) solution to get a List from a Set and sorted against a given Comparator ? 回答1: Set<Object> set = new HashSet<Object>(); // add stuff List<Object> list = new ArrayList<Object>(set); Collections.sort(list, new MyComparator()); 回答2: Just construct it. The ArrayList has a constructor taking another Collection. Set<Foo> set = new TreeSet<Foo>(new FooComparator<Foo>()); // Fill it. List<Foo> list = new ArrayList<Foo>(set); // Here's your list with items in the same

How to find all the subsets of an array in java? [duplicate]

怎甘沉沦 提交于 2019-12-14 03:33:59
问题 This question already has answers here : Obtaining a powerset of a set in Java (25 answers) Closed 4 years ago . I need to find all the subsets of an array using java.For e.g. if we have a set {1,2,3} then i should get {},{1},{2},{3},{1,2},{2,3},{1.3},{1,2,3} 回答1: You can do this to avoid needing to recurse the solutions. public static <T> void printCombinations(T[] arr) { for(long i = 0, max = 1L << arr.length; i < max; i++) { Set<T> ts = new HashSet<>(); for(int j = 0; j < arr.length; j++)

How Set checks for duplicates? Java HashSet

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:29:13
问题 For the below code it outputs " 1 ". and second code outputs " 2 " I don't understand why this is happening. Is it because I am adding the same object? How should I achieve the desired output 2. import java.util.*; public class maptest { public static void main(String[] args) { Set<Integer[]> set = new HashSet<Integer[]>(); Integer[] t = new Integer[2]; t[0] = t[1] = 1; set.add(t); Integer[] t1 = new Integer[2]; t[0] = t[1] = 0; set.add(t); System.out.println(set.size()); } } Second Code:

How to find the index of the element in a list that first appears in another given list?

北城余情 提交于 2019-12-14 03:28:18
问题 a = [3, 4, 2, 1, 7, 6, 5] b = [4, 6] The answer should be 1. Because in a, 4 appears first in list b, and it's index is 1. The question is that is there any fast code in python to achieve this? PS: Actually a is a random permutation and b is a subset of a, but it's represented as a list. 回答1: If b is to be seen as a subset (order doesn't matter, all values are present in a ), then use min() with a map() : min(map(a.index, b)) This returns the lowest index. This is a O(NK) solution (where N is

Implementing a content-hashable HashSet in C# (like python's `frozenset`)

若如初见. 提交于 2019-12-14 03:27:34
问题 Brief summary I want to build a set of sets of items in C#. The inner sets of items have a GetHashCode and Equals method defined by their contents . In mathematical notation: x = { } x.Add( { A, B, C } ) x.Add( { A, D } ) x.Add( { B, C, A } ) now x should be{ { A, B, C }, { A, D } } In python, this could be accomplished with frozenset : x = set() x.add( frozenset(['A','B','C']) ) x.add( frozenset(['A','D']) ) x.add( frozenset(['B','C','A']) ) /BriefSummary I would like to have a hashable

Find a second largest number in a Python list [duplicate]

寵の児 提交于 2019-12-14 03:05:11
问题 This question already has answers here : How to find second largest number in a list? (13 answers) Closed 6 months ago . I was trying to find the second largest number in a list and thought of converting list into set, and to convert it back to list to eliminate the repeated values. Unfortunately, the negative value took the last index of the set (where I expected the greatest value to occupy the last index in the set). List contains both negative and positive values. When it was converted to