Re: Newcomer to Python tutorial question
Terry Reedy tjre...@udel.edu wrote in message news:mailman.5248.1241732704.11746.python-l...@python.org... Alan Cameron wrote: why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? It appears that I used a reserved term when I used 'sequence'. No and Sort-of. No: We often use it in the normal English sense of ordered items, as I and I think others assume you did. Your question is quite legitimate, and the answer, as indicated, is how an implementation interacts with the sequence of additions. Sort-of: The library manual section of Sequence Types lists the sequence operations common to all or most built-in Python sequence classes. But it does not explicitly define sequence. Ranges, which are iterables that directly support only indexing and len(), are called sequences. Dicts, which are iterables that support len() but are usually not indexed by integers, are not. So that suggests a minimal definition of sequence, but all the other sequence classes support much more that is typically assumed. Keywords are reserved terms in the language such as 'if' and 'None' that are specially recognized by the parser and which affect compilation. Identifiers of the form '__x...y__' are reserved names. Non-terminal terms in the grammar are reserved terms, in a sense, within the reference manual, but 'expression_list', not 'sequence', is used for comma-separated sequences of expressions in code. The comma-separated sequence of items in a function call is separately defined as an 'argument_list' because 'keyword_item's like 'a=b' and '*' and '**' are not expressions and because there are some order restrictions on argument items. Terry Jan Reedy Thanks for the explanation. In particular reference to the tutorial section http://docs.python.org/3.0/tutorial/datastructures.html#nested-list-comprehensions There is a word which is ambiguous, at least to me. Perhaps you can explain the use of the word 'comprehensions'. Comprehension I understand Comprehensions I don't. Is there a glossary of terms somewhere? -- Alan Cameron -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
On Fri, 8 May 2009 10:16:55 +0100, Alan Cameron alan.came...@iname.com wrote: [snip] In particular reference to the tutorial section http://docs.python.org/3.0/tutorial/datastructures.html#nested-list-comprehensions There is a word which is ambiguous, at least to me. Perhaps you can explain the use of the word 'comprehensions'. Comprehension I understand Comprehensions I don't. Is there a glossary of terms somewhere? Comprehension, in this context, is a computer-science term, described in section 5.1.3 of the web page you quoted. If you look up comprehension in Wikipedia, you'll get a description of various uses of the term, including a pointer to: http://en.wikipedia.org/wiki/List_comprehension -- To email me, substitute nowhere-spamcop, invalid-net. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
On Thu, 07 May 2009 11:40:57 -0700, Chris Rebert wrote: why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Because it's *not a sequence* at all, it's a set. [pedant] But the *printed output* is a sequence. It's a sequence of characters. The OP doesn't claim that basket is a sequence-type, he is using sequence in a generic, plain English way. [/pedant] I agree with the rest of your explanation about arbitrary ordering of sets :) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Newcomer to Python tutorial question
I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? -- Alan Cameron -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
On Thu, May 7, 2009 at 11:35 AM, Alan Cameron alan.came...@iname.com wrote I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Because it's *not a sequence* at all, it's a set. Sets are unordered and contain no duplicate items, hence why the output ordering is arbitrary and only the unique subset of original elements is present. Further info: http://docs.python.org/3.0/library/stdtypes.html#set-types-set-frozenset Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron alan.came...@iname.com writes: I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? A set is an unordered container, but due to the nature of an object representation (which is a sequence of characters), its representation has to list the elements in a certain order. However, this order is not significant. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron wrote: I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? A set is not ordered and eliminates duplicate elements. So the output is random in terms of the order and only shows each single item once... Correct me if I'm wrong :-) Listick http://www.listick.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron alan.came...@iname.com wrote in message news:hrfml.50224$tb.4...@newsfe07.ams2... I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Thanks to all who replied. I assume therefore that the order in which the items of the set are printed could vary each time it is printed? -- Alan Cameron -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron wrote: I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? As already said by others, the order of items in a set is not part of the concept of a set. You can even have sets with equal contents that display differently: Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52) [GCC 4.3.3] on linux2 Type help, copyright, credits or license for more information. a = {'orange', 'banana', 'apple', 'orange', 'pear', 'apple'} b = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} a == b True repr(a) == repr(b) False a {'orange', 'pear', 'apple', 'banana'} b {'orange', 'pear', 'banana', 'apple'} Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
On Thu, May 7, 2009 at 11:58 AM, Alan Cameron alan.came...@iname.com wrote: Alan Cameron alan.came...@iname.com wrote in message news:hrfml.50224$tb.4...@newsfe07.ams2... I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Thanks to all who replied. I assume therefore that the order in which the items of the set are printed could vary each time it is printed? Due to the underlying dict-based implementation, the order will stay the same until you modify the set (i.e. add or remove an element), at which point it may change; it's basically the same behavior as with printing a dict. So this will always print the same thing twice: print basket print basket Whereas this might not: print basket #modify the set basket.discard(banana) basket.add(banana) print basket Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Chris Rebert c...@rebertia.com wrote in message news:mailman.5238.1241723354.11746.python-l...@python.org... On Thu, May 7, 2009 at 11:58 AM, Alan Cameron alan.came...@iname.com wrote: Alan Cameron alan.came...@iname.com wrote in message news:hrfml.50224$tb.4...@newsfe07.ams2... I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Thanks to all who replied. I assume therefore that the order in which the items of the set are printed could vary each time it is printed? Due to the underlying dict-based implementation, the order will stay the same until you modify the set (i.e. add or remove an element), at which point it may change; it's basically the same behavior as with printing a dict. So this will always print the same thing twice: print basket print basket Whereas this might not: print basket #modify the set basket.discard(banana) basket.add(banana) print basket Cheers, Chris Thanks Chris, It appears that I used a reserved term when I used 'sequence'. I just had not reached that far in the tutorial. I have many years of programming (roughly 50) and want to learn new languages. I find tutorials always fraught with problems due to the knowledge of the writer exceeding the knowledge of the reader and using terms and examples not yet covered in the tutorial thus far. I am persevering with my initial foray into Python. -- Alan Cameron -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron wrote: Alan Cameron alan.came...@iname.com wrote in message news:hrfml.50224$tb.4...@newsfe07.ams2... I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? Thanks to all who replied. I assume therefore that the order in which the items of the set are printed could vary each time it is printed? If you don't add or remove items the printed order will not change in the current implementation. But as shown in my other post it is possible to create sets with equal contents that are printed differently. The actual order depends on the set's history of insertions/deletions, so it is not truly random. But these are implementation details that may change across versions of python and your code should never rely on them. If you want a defined order convert the set to a sorted list before printing: basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} sorted(basket) ['apple', 'banana', 'orange', 'pear'] Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Newcomer to Python tutorial question
Alan Cameron wrote: why is the printed result of basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? It appears that I used a reserved term when I used 'sequence'. No and Sort-of. No: We often use it in the normal English sense of ordered items, as I and I think others assume you did. Your question is quite legitimate, and the answer, as indicated, is how an implementation interacts with the sequence of additions. Sort-of: The library manual section of Sequence Types lists the sequence operations common to all or most built-in Python sequence classes. But it does not explicitly define sequence. Ranges, which are iterables that directly support only indexing and len(), are called sequences. Dicts, which are iterables that support len() but are usually not indexed by integers, are not. So that suggests a minimal definition of sequence, but all the other sequence classes support much more that is typically assumed. Keywords are reserved terms in the language such as 'if' and 'None' that are specially recognized by the parser and which affect compilation. Identifiers of the form '__x...y__' are reserved names. Non-terminal terms in the grammar are reserved terms, in a sense, within the reference manual, but 'expression_list', not 'sequence', is used for comma-separated sequences of expressions in code. The comma-separated sequence of items in a function call is separately defined as an 'argument_list' because 'keyword_item's like 'a=b' and '*' and '**' are not expressions and because there are some order restrictions on argument items. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list