Commit 987d1aea authored by vermeul's avatar vermeul
Browse files

improved example

parent ba0dd162
......@@ -84,29 +84,33 @@ for root, dirs, files in os.walk('.'):
## Sorting: the `sorted` function
**Input** is the same file list as above, but we also would like to do a complex sort:
**Input** is the same list of files as above, but we also would like to apply a complex sort:
* first, sort by root
* then, sort by filename
* output: full file path
1. sort by length of filename, longest filenames first
2. sort by the lowercase of the filename
**define the sort function**
**define the sort functions**
def my_sort(root_and_filename):
root, filename = root_and_filename
def sort_by_length(filename):
return len(filename)
def sort_by_name(filename):
return filename.lower()
**apply the sort function: `sorted`**
**apply the sort functions using `sorted`**
for root, dirs, files in os.walk(path):
for filename in sorted(
filter( my_filter(coll=collection), files),
filenames = []
for root, dirs, files in os.walk('..'):
filenames += files
sorted(filenames, key=sort_by_name, reverse=False),
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment