Type List vs type ArrayList in Java [duplicate]

Almost always List is preferred over ArrayList because, for instance, List can be translated into a LinkedList without affecting the rest of the codebase. If one used ArrayList instead of List, it’s hard to change the ArrayList implementation into a LinkedList one because ArrayList specific methods have been used in the codebase that would also … Read more

How do I find the duplicates in a list and create another list with them?

To remove duplicates use set(a). To print duplicates, something like: a = [1,2,3,2,1,5,6,5,5,5] import collections print([item for item, count in collections.Counter(a).items() if count > 1]) ## [1, 2, 5] Note that Counter is not particularly efficient (timings) and probably overkill here. set will perform better. This code computes a list of unique elements in the … Read more

Finding the average of a list

For Python 3.8+, use statistics.fmean for numerical stability with floats. (Fast.) For Python 3.4+, use statistics.mean for numerical stability with floats. (Slower.) xs = [15, 18, 2, 36, 12, 78, 5, 6, 9] import statistics statistics.mean(xs) # = 20.11111111111111 For older versions of Python 3, use sum(xs) / len(xs) For Python 2, convert len to … Read more

How to iterate over a list in chunks

def chunker(seq, size): return (seq[pos:pos + size] for pos in range(0, len(seq), size)) Works with any sequence: text = “I am a very, very helpful text” for group in chunker(text, 7): print(repr(group),) # ‘I am a ‘ ‘very, v’ ‘ery hel’ ‘pful te’ ‘xt’ print(‘|’.join(chunker(text, 10))) # I am a ver|y, very he|lpful text animals … Read more

The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

The R Language Definition is handy for answering these types of questions: http://cran.r-project.org/doc/manuals/R-lang.html#Indexing R has three basic indexing operators, with syntax displayed by the following examples x[i] x[i, j] x[[i]] x[[i, j]] x$a x$”a” For vectors and matrices the [[ forms are rarely used, although they have some slight semantic differences from the [ form … Read more

Getting the index of the returned max or min item using max()/min() on a list

Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case. Avoid the solution with itemgetter() presented in the other answers, and use instead index_min = min(range(len(values)), key=values.__getitem__) because it doesn’t require to import operator nor to use enumerate, and it is … Read more

Convert list to array in Java [duplicate]

Either: Foo[] array = list.toArray(new Foo[0]); or: Foo[] array = new Foo[list.size()]; list.toArray(array); // fill the array Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way: List<Integer> list = …; int[] array = new int[list.size()]; for(int i = 0; i < list.size(); i++) array[i] = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)