Re: [Tutor] Do something on list elements

2018-07-27 Thread Mats Wichmann
On 07/27/2018 04:32 PM, Cameron Simpson wrote:
> On 27Jul2018 23:06, Alan Gauld  wrote:

>> In Python you very rarely need to resort to using indexes
>> to process the members of a collection. And even more rarely
>> do you need to manually increment the index.

I think this was an important point: use a list index only when the
actual value of the index is important, otherwise use the fact that a
list is iterable by just looping over the list itself without bothering
with indices... it's a powerful enough idea that C++. C# and other
languages picked it up.

> I use indices when I need to modify the elements of a list in place. The
> list comprehension makes a shiny new list. That is usually fine, but not
> always what is required.

True indeed, but in this case since the elements being modified are
strings, which are immutable and thus "modifying" a string creates a new
string, we're not really buying anything by not creating a new list of
those new strings along the way...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Do something on list elements

2018-07-27 Thread Alan Gauld via Tutor
On 27/07/18 23:32, Cameron Simpson wrote:

>> for index, s in l:
>>   l[index] = s.replace('X','')
>> print(l)
> 
> I think you meant:
> 
>   for index, s in enumerate(l):

Oops, yes. Sorry.

>> In Python you very rarely need to resort to using indexes
>> to process the members of a collection. And even more rarely
>> do you need to manually increment the index.
> 
> I use indices when I need to modify the elements of a list in place. 

Yes, that's probably the most common use case, and
enumerate is the best way to do that.

> comprehension makes a shiny new list. That is usually fine, but not always 
> what 
> is required.

>From a purist point of view its usually preferable but in
practice making copies of large data structures is usually
a bad idea.  In that case you are forced to resort to
enumerate, I agree.

> The other place I use enumerate in a big way is to track line numbers in 
> files, 
> as context for error messages (either now or later). For example:

Yes, but then you don't use the index to access/process
the data. Its just providing context.


-- 
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] Do something on list elements

2018-07-27 Thread Cameron Simpson

On 27Jul2018 23:06, Alan Gauld  wrote:

On 27/07/18 13:56, Valerio Pachera wrote:


l = ['unoX', 'dueX']
c = 0
for n in l:
l[c] = l[c].replace('X','')
c = c + 1
print (l)

it works but I wonder if there's a better way to achieve the same.


Yes, a much better way.

for index, s in l:
  l[index] = s.replace('X','')
print(l)


I think you meant:

 for index, s in enumerate(l):


But better still is a list comprehension:

l = [s.replace('X','') for s in l)
print(l)

In Python you very rarely need to resort to using indexes
to process the members of a collection. And even more rarely
do you need to manually increment the index.


I use indices when I need to modify the elements of a list in place. The list 
comprehension makes a shiny new list. That is usually fine, but not always what 
is required.


I also have (rare) occasions where an item wants know its own index. In that 
case one wants the index floating around so it can be attached to the item.


The other place I use enumerate in a big way is to track line numbers in files, 
as context for error messages (either now or later). For example:


 with open(filename) as f:
   for lineno, line in enumerate(f, 1):
 if badness:
   print("%s:%d: badness happened" % (filename, lineno), file=sys.stderr)
   continue
 ... process good lines ...

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Do something on list elements

2018-07-27 Thread Alan Gauld via Tutor
On 27/07/18 13:56, Valerio Pachera wrote:

> l = ['unoX', 'dueX']
> c = 0
> for n in l:
> l[c] = l[c].replace('X','')
> c = c + 1
> print (l)
> ---
> 
> it works but I wonder if there's a better way to achieve the same.

Yes, a much better way.

for index, s in l:
   l[index] = s.replace('X','')
print(l)

But better still is a list comprehension:

l = [s.replace('X','') for s in l)
print(l)

In Python you very rarely need to resort to using indexes
to process the members of a collection. And even more rarely
do you need to manually increment the index.

-- 
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] how to change the command "string" on a tkinter Button?

2018-07-27 Thread Alan Gauld via Tutor
On 27/07/18 11:55, Shall, Sydney wrote:
> On 01/07/2018 11:19, Steven D'Aprano wrote:
>> Even better would be to learn a form of VCS (version control system)
>> such as Mercurial (hg) or git. Depending on the text editor you are
>> using, it may have VCS integration available.
> 
> Does Spyder have a VCS?

Dunno, sorry...

> Could you list a few text-editors that do have VCS, please.

It's not so much that they have a VCS but that they integrate
with an existing VCS. So they will typically have menu options
for checking in/out a file or locking/unlocking it. Typically
you can configure which VCS they use from a list of common
options, in some cases you can define the command line to
use for each menu action.

vim, emacs, Eclipse, Netbeans, VisualStudio etc all
support VCS to varying degrees.

But you still need to install and configure a VCS
engine such as CVS, SVN, Hg or git etc.

-- 
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] Do something on list elements

2018-07-27 Thread Valerio Pachera
Hi all, I started studying python and I hope you may help me getting better.

Let's start with the first question.
Consider this example

---
#!/usr/bin/python3

l = ['unoX', 'dueX']
c = 0
for n in l:
l[c] = l[c].replace('X','')
c = c + 1
print (l)
---

it works but I wonder if there's a better way to achieve the same.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-27 Thread Shall, Sydney

On 01/07/2018 11:19, Steven D'Aprano wrote:

On Sun, Jul 01, 2018 at 03:32:59PM +1000, Chris Roy-Smith wrote:





"Save As..." before engaging in big changes is your friend :-)

Even better would be to learn a form of VCS (version control system)
such as Mercurial (hg) or git. Depending on the text editor you are
using, it may have VCS integration available.




Does Spyder have a VCS?
Could you list a few text-editors that do have VCS, please.


--

_

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Questions about the formatting of docstrings

2018-07-27 Thread Mats Wichmann
On 07/27/2018 11:23 AM, boB Stepp wrote:
> On Fri, Jul 27, 2018 at 12:50 AM Steven D'Aprano  wrote:
>>
>> On Thu, Jul 26, 2018 at 11:34:11PM -0500, boB Stepp wrote:
> 
>>> (1) The author claims that reStructuredText is the official Python
>>> documentation standard.  Is this true?  If yes, is this something I
>>> should be doing for my own projects?
>>
>> Yes, it is true. If you write documentation for the Python standard
>> library, they are supposed to be in ReST. Docstrings you read in
>> the interactive interpreter often aren't, but the documentation you read
>> on the web page has all been automatically generated from ReST text
>> files.
> 
> What tool is being used to generate the documentation from the ReST text 
> files?

I'm not sure if Python is still using it, but Sphinx is a popular
choice, originally written to handle the Python docs.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Questions about the formatting of docstrings

2018-07-27 Thread boB Stepp
On Fri, Jul 27, 2018 at 12:50 AM Steven D'Aprano  wrote:
>
> On Thu, Jul 26, 2018 at 11:34:11PM -0500, boB Stepp wrote:

> > (1) The author claims that reStructuredText is the official Python
> > documentation standard.  Is this true?  If yes, is this something I
> > should be doing for my own projects?
>
> Yes, it is true. If you write documentation for the Python standard
> library, they are supposed to be in ReST. Docstrings you read in
> the interactive interpreter often aren't, but the documentation you read
> on the web page has all been automatically generated from ReST text
> files.

What tool is being used to generate the documentation from the ReST text files?

Thanks!
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor