How do you cast a List of supertypes to a List of subtypes?

Simply casting to List<TestB> almost works; but it doesn’t work because you can’t cast a generic type of one parameter to another. However, you can cast through an intermediate wildcard type and it will be allowed (since you can cast to and from wildcard types, just with an unchecked warning): List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;

Quickest way to compare two generic lists for differences

Use Except: var firstNotSecond = list1.Except(list2).ToList(); var secondNotFirst = list2.Except(list1).ToList(); I suspect there are approaches which would actually be marginally faster than this, but even this will be vastly faster than your O(N * M) approach. If you want to combine these, you could create a method with the above and then a return statement: … Read more

Split a Pandas column of lists into multiple columns

You can use the DataFrame constructor with lists created by to_list: import pandas as pd d1 = {‘teams’: [[‘SF’, ‘NYG’],[‘SF’, ‘NYG’],[‘SF’, ‘NYG’], [‘SF’, ‘NYG’],[‘SF’, ‘NYG’],[‘SF’, ‘NYG’],[‘SF’, ‘NYG’]]} df2 = pd.DataFrame(d1) print (df2) teams 0 [SF, NYG] 1 [SF, NYG] 2 [SF, NYG] 3 [SF, NYG] 4 [SF, NYG] 5 [SF, NYG] 6 [SF, NYG] df2[[‘team1′,’team2’]] … Read more

Split a List into smaller lists of N size [duplicate]

I would suggest to use this extension method to chunk the source list to the sub-lists by specified chunk size: /// <summary> /// Helper methods for the lists. /// </summary> public static class ListExtensions { public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize) { return source .Select((x, i) => new { Index = i, Value … Read more

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