To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

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**
```python
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`**
```python
for root, dirs, files in os.walk(path):
for filename in sorted(
filter( my_filter(coll=collection), files),
key=my_sort(coll=collection)
):
print(filename)
filenames = []
for root, dirs, files in os.walk('..'):
filenames += files
sorted(
sorted(filenames, key=sort_by_name, reverse=False),
key=sort_by_length,
reverse=True
)
```
......
Markdown is supported
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