if else in a list comprehension [duplicate]
>>> xs = [22, 13, 45, 50, 98, 69, 43, 44, 1] >>> [x+1 if x >= 45 else x+5 for x in xs] [27, 18, 46, 51, 99, 70, 48, 49, 6] Do-something if <condition>, else do-something else.
>>> xs = [22, 13, 45, 50, 98, 69, 43, 44, 1] >>> [x+1 if x >= 45 else x+5 for x in xs] [27, 18, 46, 51, 99, 70, 48, 49, 6] Do-something if <condition>, else do-something else.
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
Given a string sentence, this stores each word in a list called words: words = sentence.split()
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
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
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 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
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
my_list = [‘a’, ‘b’, ‘c’, ‘d’] my_string = ‘,’.join(my_list) ‘a,b,c,d’ This won’t work if the list contains integers And if the list contains non-string types (such as integers, floats, bools, None) then do: my_string = ‘,’.join(map(str, my_list))
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