Newcomer to Python tutorial question

C

Chris Rebert

On Thu, May 7, 2009 at 11:35 AM, Alan Cameron <[email protected]>
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

{'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
 
F

Florian Wollenschein

A

Arnaud Delobelle

Alan Cameron said:
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

{'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.
 
P

Peter Otten

Alan said:
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

{'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.{'orange', 'pear', 'banana', 'apple'}

Peter
 
C

Chris Rebert

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
 
P

Peter Otten

Alan said:
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:
['apple', 'banana', 'orange', 'pear']

Peter
 
A

Alan Cameron

Chris Rebert said:
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.
 
T

Terry Reedy

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
 
A

Alan Cameron

Terry Reedy said:
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?
 
P

Peter Pearson

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
 
S

Steven D'Aprano

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top