Hello, Since I wanted to have only the 5 last entries of a folder containing news, i did this little script and used it in a ZPT : sorted_lst = sequence.sort(lst, (('date', 'cmp', 'desc'), ('titre', 'nocase', 'asc'))) if len(sorted_lst) <= 5 : return sorted_lst else : return sorted_lst[0:5] It works well, I've got what I want ... But I wonder why should I use sorted_lst[0:5] to have 5 elements since there is in fact 6 elements in it ?? I suppose sorted_lst[0] is some kind of a dummy element to stock some informations ... Am I right ? -- (°> Nicolas Évrard / ) Liège - Belgique ^^
Nicolas Évrard wrote:
But I wonder why should I use sorted_lst[0:5] to have 5 elements since there is in fact 6 elements in it ?? I suppose sorted_lst[0] is some kind of a dummy element to stock some informations ... Am I right ?
The sorted_lst[0:5] notation is called a slice, and returns the first 5 elements in the list.Read it as "from and including 0 until but not including 5." It only returns 5 elements. While this seems intuitive at first glance, you will get to like this as you program more. It simply fits most problems better than it would if it included the last element. (You would have "1 off" errors constantly in your code) regards max M -- "Sorry I would Really Like To Help More On This Project, But Am To Busy Doing Paid Work On A Tight Deadline" Max M
* Max M [08:38 13/08/02 CEST]:
Nicolas Évrard wrote:
But I wonder why should I use sorted_lst[0:5] to have 5 elements since there is in fact 6 elements in it ?? I suppose sorted_lst[0] is some kind of a dummy element to stock some informations ... Am I right ?
The sorted_lst[0:5] notation is called a slice, and returns the first 5 elements in the list.Read it as "from and including 0 until but not including 5." It only returns 5 elements.
Yep ! Now that we are in the morning, I wonder how I could have write such a thing befor even thinking about it ... The best part is that I use it very often but for some reason my brain stopped working yesterday near 2 O'clock in the morning ... Thanks anyway !!
While this seems intuitive at first glance, you will get to like this as you program more. It simply fits most problems better than it would if it included the last element. (You would have "1 off" errors constantly in your code)
Indeed ... -- (°> Nicolas Évrard / ) Liège - Belgique ^^
participants (2)
-
Max M -
Nicolas Évrard