In [ ]:
# slicing list with steps
my_list = [1, 3, 5, 7, 9, 2, 4, 6, 8]
my_list[1:8:3]
In [ ]:
# list comprehension with selected indexes over two lists
my_list = [1, 3, 5, 7, 9, 2, 4, 6, 8]
my_second_list = ['a', 'b', 'd', 'e', 'g', 'q', 'j', 's', 'b']
my_indexes = [0, 3, 7]
[my_list[i] for i in my_indexes]
In [ ]:
# sorting list and getting back sorted indexes to be used to order other list using third party library numpy
import numpy # numpy will need to be installed first
sorted_list = numpy.sort(my_list)
print(sorted_list)
my_sorted_indexes = numpy.argsort(my_list)
print(my_sorted_indexes)
[my_second_list[i] for i in my_sorted_indexes]
In [ ]:
# sorting list and getting back sorted indexes to be used to order other list using enumerate() and sorted()
my_sorted_indexes = [i for (v, i) in sorted((v, i) for (i, v) in enumerate(my_list))]
print(my_sorted_indexes)
[my_second_list[i] for i in my_sorted_indexes]
In [ ]:
# concatenating two lists using '+' symbol
seq = ['AAA', 'TTT']
stop = ['STOP']
new_seq = seq + stop
print(new_seq)
In [ ]:
# using regular expression to find all occurrences of the letter 'e' in a string
import re
my_text = "Check occurrences within this string.";
for m in re.finditer('e', my_text):
print(m.start(), m.end())
In [ ]:
# To execute python code on the command line
# run on the command line without '!'
# '!' symbol is used in jupyter notebook to run shell commands instead of python
!python -c "print('hello')"