Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Clarification of notation

Reply
Thread Tools

Clarification of notation

 
 
Bruce Whealton
Guest
Posts: n/a
 
      09-30-2010
Hello all,
I recently started learning python. I am a bit thrown by a
certain notation that I see. I was watching a training course on
lynda.com and this notation was not presented. For lists, when would
you use what appears to be nested lists, like:
[[], [], []]
a list of lists?
Would you, and could you combine a dictionary with a list in this fashion?

Next, from the documentation I see and this is just an example (this
kind of notation is seen elsewhere in the documentation:

str.count(sub[, start[, end]])
This particular example is from the string methods.
Is this a nesting of two lists inside a a third list? I know that it
would suggest that some of the arguments are optional, so perhaps if
there are 2 items the first is the sub, and the second is start? Or did
I read that backwards?
Thanks,
Bruce
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      09-30-2010
On 2010-09-30, Bruce Whealton <(E-Mail Removed)> wrote:
> Next, from the documentation I see and this is just an example (this
> kind of notation is seen elsewhere in the documentation:


> str.count(sub[, start[, end]])
> This particular example is from the string methods.
> Is this a nesting of two lists inside a a third list?


No, it's not -- it's a different use of [] to indicate that things
are optional, a convention which dates back to long before Python
existed.

>I know that it
> would suggest that some of the arguments are optional, so perhaps if
> there are 2 items the first is the sub, and the second is start? Or did
> I read that backwards?


That is exactly correct. The key is the implication that you can omit
end, or both start and end. (But you can't omit start and provide end.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      09-30-2010
Bruce Whealton <(E-Mail Removed)> wrote:
> For lists, when would
> you use what appears to be nested lists, like:
> [[], [], []]
> a list of lists?


Well, you'd use it when you'd want a list of lists

There's nothing magical about a list of lists, it's just a list with
objects inside like any other, in this case they just happen to be
lists. Possibly the canonical example is for a simple multidimensional
array:

>>> array5x5 = [[0]*5 for i in range(5)]
>>> array5x5[2][3] = 7
>>> array5x5[4][1] = 2
>>> array5x5

[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 7, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0]]

> Would you, and could you combine a dictionary with a list in this fashion?


Lists can contain dictionaries that contain dictionaries containing
lists So yes, they can easily be combined.

Here's a list with dictionaries:

elements = [
{'tag': 'strong', 'style': 'bold'},
{'tag': 'header', 'style': 'bolder', 'color': 'red},
]

And a dictionary of lists:

classes_2010 = {
'economics': ['John Crowley', 'Jack Savage', 'Jane Austen'],
'voodoo economics': ['Ronald Reagan', 'Ferris Beuller'],
}

(Note that the formatting style is a personal taste and not
essential).

Generally, you tend to use a list when you want to work on items in
sequence, and a dictionary when you want to work on an item on demand.

> Next, from the documentation I see and this is just an example (this
> kind of notation is seen elsewhere in the documentation:
>
> str.count(sub[, start[, end]])
> This particular example is from the string methods.
> Is this a nesting of two lists inside a a third list? *I know that it
> would suggest that some of the arguments are optional, so perhaps if
> there are 2 items the first is the sub, and the second is start? *Or did


In documentation (as opposed to code), [] represents optional
arguments, and have nothing at all to do with Python lists. The above
example is showing that the method can be called in the following
ways:

'foobarbazbam'.count('ba')
'foobarbazbam'.count('ba', 6)
'foobarbazbam'.count('ba', 6, 9)

Hope this helps.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Infix notation to postfix notation Tameem C Programming 454 01-31-2014 06:01 PM
Hungarian Notation Vs. Pascal Notation? Grey Squirrel ASP .Net 6 03-21-2007 09:42 AM
hex notation =?ISO-8859-1?Q?Andreas_H=F6lscher?= VHDL 6 12-12-2003 06:05 PM
hex notation Andreas H?lscher VHDL 2 12-10-2003 12:56 PM
Dot notation V Bracket notation Robert Mark Bram Javascript 3 07-05-2003 03:59 AM



Advertisments