Created
August 9, 2018 13:11
-
-
Save silviaclaire/bd8bd3f288d7cf410a315fcbbecd805b to your computer and use it in GitHub Desktop.
Prepare tree diagram data from cluster file list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
# get filename list | |
list = [os.path.basename(f) for f in glob.glob('./test/*')] | |
#['1-2.csv', '1-1.csv', '0.csv', '1-2-2.csv', '1.csv', '1-2-1.csv', '2.csv'] | |
# remove 0.csv and extension | |
list.remove('0.csv') | |
list = [x.replace('.csv', '') for x in list] | |
#['1-2', '1-1', '1-2-2', '1', '1-2-1', '2'] | |
new_list = [] | |
level = 3 | |
for x in list: | |
# split filename to sub list | |
temp = [int(i) for i in (x.split("-"))] | |
#[[1, 2], [1, 1], [1, 2, 2], [1], [1, 2, 1], [2]] | |
# fill empty cell with 0 | |
while len(temp) < level: | |
temp.append(0) | |
new_list.append(temp) | |
#[[1, 2, 0], [1, 1, 0], [1, 2, 2], [1, 0, 0], [1, 2, 1], [2, 0, 0]] | |
new_list.sort() | |
#[[1, 0, 0], [1, 1, 0], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 0]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment