Split a list into sub-lists based on index ranges

In python, it’s called slicing. Here is an example of python’s slice notation: >>> list1 = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’, ‘i’, ‘j’, ‘k’, ‘l’] >>> print list1[:5] [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] >>> print list1[-7:] [‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’] Note how you can slice either positively or negatively. When you use a negative number, it … Read more

list the subfolders in a folder – Matlab (only subfolders, not files)

Use isdir field of dir output to separate subdirectories and files: d = dir(pathFolder); isub = [d(:).isdir]; %# returns logical vector nameFolds = {d(isub).name}’; You can then remove . and .. nameFolds(ismember(nameFolds,{‘.’,’..’})) = []; You shouldn’t do nameFolds(1:2) = [], since dir output from root directory does not contain those dot-folders. At least on Windows.

Python: Rename duplicates in list with progressive numbers without sorting list

My solution with map and lambda: print map(lambda x: x[1] + str(mylist[:x[0]].count(x[1]) + 1) if mylist.count(x[1]) > 1 else x[1], enumerate(mylist)) More traditional form newlist = [] for i, v in enumerate(mylist): totalcount = mylist.count(v) count = mylist[:i].count(v) newlist.append(v + str(count + 1) if totalcount > 1 else v) And last one [v + str(mylist[:i].count(v) … Read more

Arrays.asList(int[]) not working [duplicate]

When you pass an array of primitives (int[] in your case) to Arrays.asList, it creates a List<int[]> with a single element – the array itself. Therefore contains(3) returns false. contains(array) would return true. If you’ll use Integer[] instead of int[], it will work. Integer[] array = {3, 2, 5, 4}; if (Arrays.asList(array).contains(3)) { System.out.println(“The array … Read more

How to convert a list with properties to a another list the java 8 way?

you will have to use something like below : List<ProductManager> B = A.stream() .map(developer -> new ProductManager(developer.getName(), developer.getAge())) .collect(Collectors.toList()); // for large # of properties assuming the attributes have similar names //other wise with different names refer this List<ProductManager> B = A.stream().map(developer -> { ProductManager productManager = new ProductManager(); try { PropertyUtils.copyProperties(productManager, developer); } catch … Read more

Using map() for columns in a pandas dataframe

The simpliest is use lambda function with apply: df = pd.DataFrame({‘col1’:pd.date_range(‘2015-01-02 15:00:07’, periods=3), ‘col2’:pd.date_range(‘2015-05-02 15:00:07’, periods=3), ‘col3’:pd.date_range(‘2015-04-02 15:00:07’, periods=3), ‘col4’:pd.date_range(‘2015-09-02 15:00:07’, periods=3), ‘col5’:[5,3,6], ‘col6’:[7,4,3]}) print (df) col1 col2 col3 \ 0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07 1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07 2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07 col4 col5 col6 0 … Read more