Lists Flashcards

1
Q

last 10 elements in list el_list

A

el_list[-10:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Get index and value of maximum element in the list l1

A

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))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

create List of nonzero elements of list a

2 ways

A

1) nonzeroind =[i for i, e in enumerate(a) if e != 0]

2) nonzeroind = np.nonzero(a)[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

find categorical columns after defining numerical columns

A
numeric_cols = ['RFCD.Percentage.1', 'RFCD.Percentage.2']
categorical_cols = list(set(X.columns.values.tolist()) - set(numeric_cols))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Count number of unique elements in list

A

from collections import Counter

orig = [‘a’, ‘b’, ‘a’, ‘c’, ‘b’, ‘a’, ‘c’]

res = Counter(orig)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

1) Make numpy array from list_l1

2) Make list from numpy array np_l1

A

1) np_l1 = numpy.array(list_l1)

2) list_l1 = list(np_l1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reverse list

A

brr[:] = brr[::-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly