| @@ -1,25 +1,23 @@ | | | @@ -1,25 +1,23 @@ |
1 | When you try to sort a list of strings that contain numbers, the | | 1 | When you try to sort a list of strings that contain numbers, the |
2 | normal python sort algorithm sorts lexicographically, so you might | | 2 | normal python sort algorithm sorts lexicographically, so you might |
3 | not get the results that you expect: | | 3 | not get the results that you expect: |
4 | | | 4 | |
5 | >>> a = ['a2', 'a9', 'a1', 'a4', 'a10'] | | 5 | >>> a = ['a2', 'a9', 'a1', 'a4', 'a10'] |
6 | >>> sorted(a) | | 6 | >>> sorted(a) |
7 | ['a1', 'a10', 'a2', 'a4', 'a9'] | | 7 | ['a1', 'a10', 'a2', 'a4', 'a9'] |
8 | | | 8 | |
9 | Notice that it has the order (‘1’, ‘10’, ‘2’) - this is because the | | 9 | Notice that it has the order ('1', '10', '2') - this is because the |
10 | list is being sorted in lexicographical order, which sorts numbers | | 10 | list is being sorted in lexicographical order, which sorts numbers |
11 | like you would letters (i.e. ‘b’, ‘ba’, ‘c’). | | 11 | like you would letters (i.e. 'b', 'ba', 'c'). |
12 | | | 12 | |
13 | natsort provides a function natsorted that helps sort lists | | 13 | natsort provides a function natsorted that helps sort lists |
14 | “naturallyâ€, either as real numbers (i.e. signed/unsigned floats | | 14 | "naturally", either as real numbers (i.e. signed/unsigned floats |
15 | or ints), or as versions. Using natsorted is simple: | | 15 | or ints), or as versions. Using natsorted is simple: |
16 | | | 16 | |
17 | >>> from natsort import natsorted | | 17 | >>> from natsort import natsorted |
18 | >>> a = ['a2', 'a9', 'a1', 'a4', 'a10'] | | 18 | >>> a = ['a2', 'a9', 'a1', 'a4', 'a10'] |
19 | >>> natsorted(a) | | 19 | >>> natsorted(a) |
20 | ['a1', 'a2', 'a4', 'a9', 'a10'] | | 20 | ['a1', 'a2', 'a4', 'a9', 'a10'] |
21 | | | 21 | |
22 | natsorted identifies numbers anywhere in a string and sorts them | | 22 | natsorted identifies numbers anywhere in a string and sorts them |
23 | naturally. Here are some other things you can do with natsort (please | | 23 | naturally. |
24 | see the examples for a quick start guide, or the api for more | | | |
25 | details). | | | |