Re: [Tutor] Range command
On 27/06/2019 00:07, Brick Howse via Tutor wrote: > Hello all, > > New to programming and I noticed the range command did not function like it > does in the tutorial. > For example, > I type > range(5, 10) > And the output is > range(5, 10) You have a Python 2 tutorial but are using Python 3. In Python 3 range has changed and returns a fancy kind of object (a range) that you don't really need to know about. If you really want the values convert it to a list: >>> list(range(5,10)) > Any suggestions is greatly appreciated. To avoid any similar confusion either find a Python 3 tutorial or download Python 2 and use it. Once you know Python 2 converting to Python 3 is not hard. But it is probably best just to learn Python 3 from the start! Thee are quite a few of these kinds of changes between Python 2 and 3. For experienced programmers they are "A Good Thing" because they make Python more efficient and more consistent in the way it works. But for a beginner they just seem bizarre and unintuitive. For now, just accept them and eventually you will understand why they exist. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Range command
On 6/26/19 5:07 PM, Brick Howse via Tutor wrote: > Hello all, > > New to programming and I noticed the range command did not function like it > does in the tutorial. > For example, > I type > range(5, 10) > And the output is > range(5, 10) > > In section 4.3 > It shows it should display > range(5, 10) > 5, 6, 7, 8, 9 > > I'm using windows 10 > Python 3.7.3 > > Any suggestions is greatly appreciated. Things change. You have a Python2-oriented tutorial. Neither version is wrong. In Python2, range returns a list. In Python3, range returns a range object, which you can iterate over. So for a typical use, which is to iterate over the range, the results are identical: >>> for i in range(5,10): ... print(i) ... 5 6 7 8 9 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
On 26Jun2019 11:01, Mats Wichmann wrote: On 6/26/19 4:40 AM, mhysnm1...@gmail.com wrote: Link lists I would guess be useful in an index for a database? I believe the "classic" use case is memory management - keeping track of chunks of memory. Flipping this, your typical ordered database index (unless the tech has advanced further) is a B+ tree possibly with a doubly linked list threaded through the leaf nodes. So a B+ tree is a sorted tree structure which is maintained in a balanced way so that the depth is pretty consistent across the tree, in turn so that there is not particularly long branch equivalent to have particular keys which are very expensive to look up. Unlike a binary tree a B+ tree has many keys at each node because this makes for efficient storage of the nodes in "blocks", whatever size is useful, and is also makes the tree shallower, making lookups cheaper. The leaf nodes point at the associated records (or possibly just hold keys if there are no records), and the doubly linked list of leaf nodes means you can traverse forwards or backwards from one leaf to the next in either order. Which makes find ranges of keys efficient: "SELECT ... WHERE key >= value ORDER BY key DESC" and its many variants. As has been pointed out by others, the various computer science basic structures are often built up into something different or more complex depending on what your data access pattern will be. It is important to understand the basic structures so that you can reason about their trade offs, and in turn understand more complex related structures. This goes both ways: in design you choose a structure to support what you're going to do. In use, you might choose to approach a problem differently depending on what data structure is used to arrange the data, because some actions are cheap in some structures and expensive in others, so you choose the efficient action where possible. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Range command
Hello all, New to programming and I noticed the range command did not function like it does in the tutorial. For example, I type range(5, 10) And the output is range(5, 10) In section 4.3 It shows it should display range(5, 10) 5, 6, 7, 8, 9 I'm using windows 10 Python 3.7.3 Any suggestions is greatly appreciated. Thank you in advance. Derek Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
On 26/06/2019 19:46, Mats Wichmann wrote: > I forgot to add the snide-comment part of "what are these good for": > > (a) binary search trees are excellent for Computer Science professors > who want to introduce recursion into their classes. > > (b) all the classic data structures are candidates for being job > interview questions ("implement a linked list in Python on the whiteboard") That's a good point that I intended to make in my response but forgot. In practice, Python's data structure can mimic these classic data structures and do so more efficiently than trying to write them from scratch in Python. So while they all have Computer Science theoretic pros and cons, in practice, Python programmers tend to ignore them and use lists, sets, tuples and dictionaries. And if they need anything more complex classes or pre-built modules. It's a very rare problem that needs a traditional data structure in Python. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
On 6/26/19 11:59 AM, William Ray Wing via Tutor wrote: > One of the most useful (to me) structures is the double-ended queue ("from > collections import deque”). It creates a queue that can quickly remove an > item from one end and add an item to the other. Particularly useful for > displaying a sliding window into time series data, or a moving track of the > most recent n observations of a physical measurement. > > Bill Indeed. deques are often implemented using double-linked lists, the Python version is (though you can't get at the details easily). I forgot to add the snide-comment part of "what are these good for": (a) binary search trees are excellent for Computer Science professors who want to introduce recursion into their classes. (b) all the classic data structures are candidates for being job interview questions ("implement a linked list in Python on the whiteboard") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
> On Jun 26, 2019, at 6:40 AM, mhysnm1...@gmail.com wrote: > > All, > > > > General computer science question for data structures. > > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. I > am not after code, just explanation. > One of the most useful (to me) structures is the double-ended queue ("from collections import deque”). It creates a queue that can quickly remove an item from one end and add an item to the other. Particularly useful for displaying a sliding window into time series data, or a moving track of the most recent n observations of a physical measurement. Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
On 6/26/19 4:40 AM, mhysnm1...@gmail.com wrote: > All, > > > > General computer science question for data structures. > > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. I > am not after code, just explanation. > > > > Link lists > > Double link-lists This becomes a question of what kind of data you have and what you need to do with it. Python has "lists", which are what most people think of as arrays - you reference them by index. Lists have fast access: if you know you want the 7th element, you just ask for that. Then if you want to insert a new element following that, the whole array past that point has to move to make room - the grunt work is normally abstracted away by languages that have array datatypes, but it is still happening, same for deleting, so lots of random inserts/deletes (except at the end) are "expensive". In linked lists, insert is cheap. You just take the existing element's "next" field and stick that into the new element's "next" field, and then stick the new element into the existing element's "next" field. However, deleting an still isn't cheap: unless your traversal mechansm saves it on the way to finding a certain element, you have to start from the beginning and walk through to find the previous element, because it's this one which has to have its "next" field changed. A doubly linked list however makes removal trivial - you already have a reference to that element via the "previous" field. For both kinds of linked lists, searching is expensive as you have to walk the chain. The fastest data structure for searching is when the lookup is hashed - this is what the Python dict type allows for, as the keys are hashed as they're stored (leading to the sometimes surprising rule for Python dicts that "key must be hashable"). Other popular data structures are queues and stacks. Note there are also circular lists, which only means the tail hooks back into the head. So your choices depend on what the usage pattern of your data is. The fact that Python doesn't have link lists, neither natively or in the standard library, suggests the designers didn't think they represented a common enough usage pattern for users of the language. > Binary trees > > What other trees are their other than hierarchical tree (file systems) Can let this go without mentioning the Merkle tree, darling of anyone who's ever dabbled in blockchain :) > Link lists I would guess be useful in an index for a database? I believe the "classic" use case is memory management - keeping track of chunks of memory. > Binary trees useful for searches? it's useful for searches if the "is A less than B" comparison is a useful property of the data you are representing. If not, it isn't useful at all as you have no way to construct the tree :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic Question about Visualization for enduser
On Tue, 25 Jun 2019 at 17:08, Sinardy Xing wrote: > > My question is, how currently all of this great technology glue together > and as a final product for the enduser. Because I cant imagine that we > install Anaconda Jupyter Notebook at frontend for the enduser to use it, > and give end user bunch of *.py It really depends who the end user is and what you want to give to them. To you it might seem obvious what work you are doing or what you mean by a "final product" or an "end user" but to me those can mean all sorts of things. Jupyter notebooks are great for sharing the results with other people who might want to tweak the code and try test variations of it. It's popular for data visualisation because you can encapsulate both the code and the output plots in a single file that you can send to someone or share on the web etc. On the other hand if your end user is not someone who will want to run the code but just wants to see the output plots etc then you can turn the notebook into a PDF file and give that to them. I often use matplotlib to make plots that I save as PDFs and then incorporate into a larger PDF document that I create using LaTeX. That's a common pattern for matplotlib which aims to create "publication quality figures" using vector graphics. If you actually wanted to create an interactive piece of software for other people to use on their computers then that's very different. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structures general query
On 26/06/2019 11:40, mhysnm1...@gmail.com wrote: > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. > Link lists Link lists are very flexible and ideal for when you have a varying amount of data and don;t want to reserve space for a maximum amount when you only use a fraction of it. Examples are process lists in an OS. Jobs in a queue (although technically queues are data structures in their own right!) Think of situations where you use a list in everyday life - a shopping list. You add items in an ad-hoc way and don't know in advance how any items you will eventually have. The advantages are that they have a low memory footprint, easy to move things around (including deletion), insertion is quick if adding to the top. Searching not so much. > Double link-lists Same as single linked lists except you can access both ends so a sorted list becomes much faster to search but at the cost of making inserts and moves etc slightly more complex(but only slightly). Real world uses? Hmmm, for me not so many. I'll let somebody else suggest something! > Binary trees Good for searching. Not so good for insertions/moves. Always a danger of the tree becoming unbalanced in which case it tends towards becoming a linked list. Real world use - heirarchical or ordered data arriving at random. Trees are inherently ordered so inserting the data puts it "in order" Code tends to be more complex than for lists though and is often recursive in nature which can be an issue for big trees. > What other trees are their other than hierarchical tree (file systems) Thee are whole families of trees including multi child trees such as yours. AVL trees (if memory serves) try to force the tree to be balanced. Non binary trees need selector functions etc (often hash based). Real world use - database caches, network management heirarchies parsing structures. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tree or link-list questions.
On 26/06/2019 11:34, mhysnm1...@gmail.com wrote: > The reason why I am using the tree structure like a file system. Is I am > going to attempt to write some code to walk down the tree based upon certain > conditions which I am still working on. Based upon the test conditions will > determine how I move up and down the tree. Defining the test conditions is the hard part of building and searching any tree. That's why binary trees are so much easier, the usual comparison (<,=,>) apply. When you have multiple subtrees your test function must return a selection index telling you which subtree to go down. > The high picture is I am trying to build a dynamic search functionality > based upon keywords in the text. Like a store name or organisation name. The > text has been cleaned up before applying it to the tree based upon text or > punctuation and digits that are not required. Such as receipt numbers, > postcodes, braces, etc. This is mostly done. I am hoping the tree structure > will help in finishing the search capability. I will only know once I have > tried it. If not, then I will look for another method. One thing to consider is an LDAP directory. It is a tree structure that acts like nested dictionaries and is specifically designed for handling hierarchies. Specifically organisational hierarchies, but any kind will do. It what's at the heart of most corporate Directory systems - such an Microsoft Exchange. And the good news is that there are open source options available and Python modules to work with them. but it might be overkill for your project. But worth a quick Google and Wikipedia browse... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] data structures general query
All, General computer science question for data structures. When would you use the below structures and why? If you can provide a real life example on when they would be used in a program This would be great. I am not after code, just explanation. Link lists Double link-lists Binary trees What other trees are their other than hierarchical tree (file systems) Link lists I would guess be useful in an index for a database? Binary trees useful for searches? Sean ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tree or link-list questions.
Allan, Once again, thanks for the help. I need to read more on OOPS it is all new and that was my first attempt. I looked at nesting Dictionaries and it got quite complex fast with the length of the strings and the path as you have outlined. I have parked that structure style for now. All these answers are really helping me to improve my programming knowledge and narrowing my definition. The reason why I am using the tree structure like a file system. Is I am going to attempt to write some code to walk down the tree based upon certain conditions which I am still working on. Based upon the test conditions will determine how I move up and down the tree. The high picture is I am trying to build a dynamic search functionality based upon keywords in the text. Like a store name or organisation name. The text has been cleaned up before applying it to the tree based upon text or punctuation and digits that are not required. Such as receipt numbers, postcodes, braces, etc. This is mostly done. I am hoping the tree structure will help in finishing the search capability. I will only know once I have tried it. If not, then I will look for another method. I am enjoying what I am doing because I am learning. I am pushing myself beyond my normal level of skills. Playing in the dark as I say. LOL Sean -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, 26 June 2019 2:48 AM To: tutor@python.org Subject: Re: [Tutor] tree or link-list questions. On 25/06/2019 13:24, mhysnm1...@gmail.com wrote: ... the tree I am trying to create is: > > * A single root node > * A node can only have a single parent. > * A parent can have multiple children. > * Each node is unique. A tree with multiple children (where multiple is more than 2) is entirely possible but does make the code more complex. Have you considered a binary tree where items go either left or right depending on whether they are "greater than" or "less than" (for some arbitrary definition of greater and less) than the parent node? This is much simpler to code, and you will find lots of examples online. -- monospace font needed --- Root the brown fox cow car yellow car green house quick brown fox yellow flowers -- end of monospace font --- The immediate questions you must answer (because only you understand the requirements) are: 1) How do you know which tree branch to navigate down? For example you have two car nodes. If searching for car which is the correct answer? Similarly for fox? 2) How do you know to go down an extra level. For example there are 4 nodes below 'the' What determines whether a new node sits alongside the existing nodes or at a level below. 3) Having decided you need to go a level lower how do you know which subnode to go down? How do you insert the node car into the correct subnode of the?(either brown or yellow) It looks like you are doing this based on the textual context. That means you insertion code has to be stateful since it must remember the previous word inserted (or have access to the original data). That gets complicated really quickly. Similarly, when you try to retrieve the data how do yu know which node to fetch. For example if I search for fox? Surely you will need to provide the full 'path' to fox topick the right one? But if you know the path why search a tree? In that case nested dictionaries would seem a better solution? Something like(missing all the quotes!): data = { the: [ {brown: [ {fox:[]}, {cow:[]}, {car:[]}, ]}, {yellow: [ {car: []} ]}, {green: [ {house: []} ]}, {quick: [ {brown: [ {fox: []} ]}, ]}, ]}, {yellow: [ {flowers: []} ]} ]} Then you can access the appropriate path: myFox = data[the][quick][brown][fox] Just a thought. Since I don't really know how you intend to use this I'm not sure if that would work for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor