简体   繁体   中英

Making directory structure in python using a trie

I have a list of filenames:

filenames = ["111", "112", "1341", "2213", "2131", "22222", "11111"]

that should be organised in a directory structure, and the maximum number of files in one directory should not be larger than let's say 2 . I therefore make a prefix tree (trie, code below) stored in a dictionary with prefixes as keys and 'end' if the amount of files in the subtree won't exceed the maximum:

trie = make_trie(filenames, max_freq=2)

trie
{'1': {'1': {'1': 'end', '2': 'end'}, '3': 'end'},'2': {'1': 'end', '2': 'end'}}

For each filename I then do a lookup (code below) in the trie and build a path accordingly:

for f in filenames:
    print("Filename: ", f, "\tPath:", get_path(f, trie))

Filename:  111  Path: 1/1/1/
Filename:  112  Path: 1/1/2/
Filename:  1341         Path: 1/3/
Filename:  2213         Path: 2/2/
Filename:  2131         Path: 2/1/
Filename:  22222        Path: 2/2/
Filename:  11111        Path: 1/1/1/

This works well, but with my naive implementations of trie ( make_trie ) and lookup ( get_path ), this becomes prohibitive. My guess is I should take an efficient existing trie implementation such as pytrie and datrie , but I don't really know how to make a trie that has the threshold value of 2 for the number of suffixes, so I'm a bit stuck in how to use the packages, eg:

import datrie
tr = datrie.Trie(string.digits) # make trie with digits
for f in filenames:
    tr[f] = "some value" # insert into trie, but what should be the values??

tr.prefixes('111211321') # I can look up prefixes now, but then what?

How can I use an existing, fast trie implementation to make my directory structure?

My naive implentations of trie and loookup:

def make_trie(words, max_freq):
    root = dict()
    for word in words:
        current_dict = root
        for i in range(len(word)):
            letter = word[i]
            current_prefix = word[:i+1]
            prefix_freq = sum(list(map(lambda x: x[:i+1]==current_prefix, words)))
            if prefix_freq > max_freq:
                current_dict = current_dict.setdefault(letter, {})
            else:
                current_dict = current_dict.setdefault(letter, "end")
                break
    return root

def get_path(image_id, trie):
    result = ""
    current_dict = trie
    for i in range(len(image_id)):
        letter = image_id[i]
        if letter in current_dict:
            result += letter + "/"
            if current_dict[letter] == "end":
                break
            current_dict = current_dict[letter]
    return result

This could work, using os.makedirs .

import os

def create_dir_structure(filenames):
    for filename in filenames:
        os.makedirs(
            '/'.join(e for e in str(filename))
        )


create_dir_structure(
    ['1111', '1123']
)

Tell me in comments if there is any different behavior you'd like to see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM