Lists Flashcards
last 10 elements in list el_list
el_list[-10:]
Get index and value of maximum element in the list l1
1)import operator
index, value = max(enumerate(l1), key=operator.itemgetter(1))
2)max_value = max(my_list) max_index = my_list.index(max_value)
2*)max_index = my_list.index(max(my_list))
create List of nonzero elements of list a
2 ways
1) nonzeroind =[i for i, e in enumerate(a) if e != 0]
2) nonzeroind = np.nonzero(a)[0]
find categorical columns after defining numerical columns
numeric_cols = ['RFCD.Percentage.1', 'RFCD.Percentage.2'] categorical_cols = list(set(X.columns.values.tolist()) - set(numeric_cols))
Count number of unique elements in list
from collections import Counter
orig = [‘a’, ‘b’, ‘a’, ‘c’, ‘b’, ‘a’, ‘c’]
res = Counter(orig)
1) Make numpy array from list_l1
2) Make list from numpy array np_l1
1) np_l1 = numpy.array(list_l1)
2) list_l1 = list(np_l1)
Reverse list
brr[:] = brr[::-1]