Thursday, November 15, 2018

[Python] How to sort python list string with number inside? - How to write natrual sort order in python?


How to sort python list string with number inside?
For example, [ "item1", "item13", "item15", "item7" ]

How to write natrual sort order in python?


There are two basic ways to sort list in python.

First, the sorted function. The sorted built-in function returns sorted list. Be careful! The list itself is not sorted. [Line6, Line7]

Second, List.sorted() method. The list.sorted() method changes list itself in sorted order.

When list item contains number with string, both sorted function and list.sorted() method does not work properly.

Both line5 and line7 show improper sorted list which is not numeric order.

To figure this out, let's call list.sort() method with lambda function. [Line12]

The list.sort() method allows "key" argument which gives list.sort() method to specify the sorting criteria.

In this example, list elements are consisted with "string" + "number".

["item7", "item1", "item15", "item13"]  [Line 4]

We want sort these elements in numeric order, like..

['item1', 'item7', 'item13', 'item15']  [Line 13]

By using built-in sort function, however, sorting process seems not working properly.

['item1', 'item13', 'item15', 'item7']  [Line 5, 7]

This because 'lexicographic sorting' compares string chracter by character, which means '7' is greater than '1'.

That's why built-in sort function sorts 'item13' is less than 'item7'.

To solve this problem, we need to set the critea to python by passing parameter called "key".

The key of sort function is the number part, not the string.

All elements in this list contain string "item" and numeric value.

By slicing element from 4th index, this can obtain numeric part.

Like element[4:]

In this way, list item with "string" + "number" can be sorted by lst.sort(key=lambda x: int(x[4:])). [Line 12]

Python List.sort() method, Key Functions.

Why do some sorting methods sort by 1, 10, 2, 3 ... ?

[Bioinformatics] How to open and count total base Fasta file in Windows?


How to open and count total base from Fasta file in Windows?


There are two answers to open and count total base from Fasta file in Windows.


Read Fasta file with Python Script.


Download FastaReader GUI v1.0

1) Download FastaReaderGui.jar

2) Launch FastaReaderGui.jar.
Click "Open a Fasta File" button.

3) Select and open Fasta file.

4) See the result.

Sunday, January 21, 2018

[Bioinformatics 101] 007. Function Method

Hello! This is Kenneth J Han!

In this post, we are going to look at function (or method).

007. Function Method


Create a function (or a method) called Factorial.
The Factorial has one integer parameter "num" and returns factorial value of "num".
Calculate the value of 3 factorial, 4 factorial and 5 factorial.


    result ← 1
    WHILE num > 0
        result ← result * num
        num ← num - 1
    RETURN result

result3 ← Factorial(3)
result4 ← Factorial(4)
result5 ← Factorial(5)

PRINT result3, result4, result5


6  24  120

If you have difficulties solving problem, visit the links below!
Your source code is on ready to serve!

Python source code answer on Github
Java source code answer on Github

See you on next post!