The document discusses different sorting algorithms and methods for sorting lists, tuples, and dictionaries in Python. It also describes how to make system calls from Python using modules like os and subprocess to run commands and capture outputs. The document concludes by explaining how to write a Python script to split a grep search into smaller groups to speed up the processing time.
The document discusses different sorting algorithms and methods for sorting lists, tuples, and dictionaries in Python. It also describes how to make system calls from Python using modules like os and subprocess to run commands and capture outputs. The document concludes by explaining how to write a Python script to split a grep search into smaller groups to speed up the processing time.
about Algorithms HORT 590 Lab 12 Instructor: Kranthi Varala Sorting a list of values
• Sorting is the process of ordering items in an
increasing (or decreasing) order based on their value. • Lists in Python can be sorted in two ways: • list.sort() function • sorted() function • sorted is a general function that will accept any iterable item, such as dictionaries, tuples etc. • Both versions use a sorting algorithm called ‘Timsort’ which is a hybrid of merge and insert sort methods. Sorting a list of values
• Remember to capture the output of sorted in a
new object Sorting tuples
• Sorting tuples by default will sort them by the first
element in the tuple. In this example, that is the name of the employee. • The sorted() function can be used to sort based on a different element by using the ‘key’ argument. • In this example, the lambda construct is used to generate an inline functions that simply returns the element at index 2. Sorting Dictionaries
• Sort by keys :
• Sort by values :
• Sort keys by values :
Sorting as an algorithm exercise
• Sorting is a classic example for discussing
algorithm complexity. • Visualize the different sorting algorithms here: https://visualgo.net/en/sorting Making system calls from python
• First, let’s load a newer version of python:
• module load intel/14.0.2.144 • module load python/2.7.6 • Now, we can use python to make calls to the system i.e., calls commands and scripts available on the system command line. • The ‘os’ and ‘subprocess’ module are the two main ways to interact with the system command line. Making system calls from python
• os.system will send the argument to the command
line and execute it. • The output is shown in the current stdout but not returned to the caller program. • Use of os.system is ‘deprecated’ Making system calls from python
• subprocess.call is the current preferred way for
system calls. • The argument shell=True sends the command without trying to first interpret it. Capturing output from system calls
• The os.environ command allows you to capture
environment variables set in the shell. • Popen along with communicate() functions lets you make a system call and capture the output in your program Splitting a grep search • Let’s create a python script to make a grep search faster. • Original command : grep –f Sites.txt SRR444602.fastq • This command will search for all patterns listed in Sites.txt in the file SRR444602.fastq. • Each of the patterns in Sites.txt is evaluated in each line of the file SRR444602.fastq • This command took 27 seconds. • Let’s simplify the search by breaking down the input patterns into smaller groups. Splitting a grep search • Let’s create a python script to make a grep search faster. • Original command : grep –f Sites.txt SRR444602.fastq • This command will search for all patterns listed in Sites.txt in the file SRR444602.fastq. • Each of the patterns in Sites.txt is evaluated in each line of the file SRR444602.fastq • This command took 27 seconds. • Let’s simplify the search by breaking down the input patterns into smaller groups. Splitting a grep search • Finish the script splitGrep.py in /scratch/scholar/k/kvarala/Week13 • Instructions for the next steps are in the file