Here I am again, same old arguments

CJ cj at nowhere.com
Thu Oct 13 10:05:00 CEST 2005


 
Wow, thanks alot. I pretty much (due to my own desire to get the program to )(@#T(=!!! work and 
be done with it) just turned the list into a function that returns a list that isn't attached to 
anything but the function itself, but I've taken the advice to heart.

Most of what you posted makes sense, and is in fact easier than what I was doing, but I have 
three questions:

1) Why no global variables? I'm taking your word for it that they're bad. Far be it from me to 
argue with you, but why are they bad ideas to begin with? Most of the languages I've used up to 
this point have been reliant on globals, so I'm not entirely sure why they shouldn't be used.

2) Why no for loop with an index? Again, far be it from me to argue, but it seemed perfect for 
my program. 

3) Where do I find a command list, with syntax and all that fun stuff for Python? I've explored 
the python site to no end, but I can't seem to find a list.


Again, thanks to everyone who put my crappy noob proggie through the blender :D I really did 
learn alot. Like how my 60 line program got turned into a 15 line code snippet. (I'm not being 
sarcastic, really, thanks)




-----
Wait a minute. I can use my PSP to play GAMES?!?





Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote in
news:pan.2005.10.09.12.15.12.523844 at REMOVETHIScyber.com.au: 

> On Sun, 09 Oct 2005 07:02:52 +0000, CJ wrote:
> 
>>    Okay, same program, different issue. Thanks to the help that I was
>> given I was able to complete my program to find variables in a list
>> that were repeated, and display them once, and how many times they
>> appeared in the list. And it worked great! 
>> 
>>    But, being the perfectionist that I am, I wanted to make the
>>    proggie 
>> allow any size of list, and not have to be recoded every time. So
>> step one was to not make the program reliant on the list itself being
>> of X length all the time.
> 
> First off -- don't use a for loop with an index as you are doing.
>  
>> #setup variables
>> grub=[3,25,3,5,3,"a","a","BOB",3,3,45,36,26,25,"a",3,3,3,"bob","BOB",6
>> 7] grubrpt=grub
>> cntro=0
>> cntrt=0
>> rpt=0
>> skipped=0
> 
> You are doing too much manual work! Let Python do the lion's share of
> the work for you!
>  
>> #set up for variable length of grub
>> ttllen=len(grub)-1
> 
> Why are you subtracting one from the length of the list?
> 
>> print "The heck is this for loop doing?" 
>> for point in range(0,ttllen,1):
> 
> Using point as a loop index is generally a bad idea. The result coming
> from range is not a point, it is an integer, so why call it a point?
> 
> You are also over-specifying the input arguments to range. If the step
> size is one, you don't need to specify it -- that's the default. You
> just make it harder to read, for no reason. Likewise the initial
> starting value of zero. Just use range(ttllen).
> 
> This, by the way, will return a list [0, 1, 2, ... , length of list -
> TWO] because you already subtracted one from the length.
> 
>>     print "Here's Grub=",grub
>>     print "And grubrpt=",grubrpt
>>     grubrpt[point]="blk"
> 
> As others have pointed out, grub and grubrpt are both names for the
> same list. Changing one changes the other.
> 
> 
>> #Makes sure that there are not multiple prints. 
>> def alrdy_dn(grub,grubrpt):
>>     if grub[cntro] in grubrpt:
> 
> Ew!!! Global variables!!!
> 
> Bad programmer! No biscuit!!!
> 
> *wink*
> 
> Global variables are almost always a BAD idea.
> 
>>         return grubrpt
>>     else:
>>         print grub[cntro],"appears in list",rpt,"times."
>>         grubrpt[grubrpt.index("blk")]=grub[cntro] return grubrpt
> 
> This is a strange function. What exactly is it meant to do? It
> combines user interface (printing the number of times each item
> appears) and functionality (counting the number of times each item
> appears) and side effects (changing the list), before returning one of
> the input arguments again.
> 
> At least two of those things (counting the items, and printing the
> results) should be separated into different functions for ease of
> comprehension.
> 
> I'm going to skip the rest of your code, because I don't understand it
> and am too lazy, er, I mean busy, to spend the time trying to decipher
> it. Especially since the function you are trying to duplicate manually
> is so easy to do if you work with Python instead of against it.
> 
> def count_item(L, item):
>     """Count the number of times item appears in list L."""
>     return L.count(item)
> 
> Or wait... that's too easy :-)
> 
> If you want to roll your own, then do it like this:
> 
> def count_item(L, item):
>     """Count the number of times item appears in list L by reinventing
>     the wheel."""
>     n = 0
>     for obj in L:
>         if obj == item:
>             n += 1
>     return n
> 
> Notice that we don't change the list at any time. Why change it? That
> just adds complexity to our program and adds extra places to make
> bugs. Of which you have many :-)
> 
> Now you use it like this:
> 
> grub=[3,25,3,5,3,"a","a","BOB",3,3,45,36,26,25,"a",3,3,3,"bob","BOB",67
> ] for item in grub:
>     n = count_item(grub, item)
>     print item, "appears in list", n, "times."
> 
> 
> And you are done.
> 
> No, not quite -- my code has a bug in it. You want to print the count
> for each *unique* item. Mine prints the count for each item,
> regardless of whether it is unique or not. So what we need to keep
> track of which items have been counted before. Here is one way of
> doing it: 
> 
> grub=[3,25,3,5,3,"a","a","BOB",3,3,45,36,26,25,"a",3,3,3,"bob","BOB",67
> ] already_seen = []
> for item in grub:
>     if item not in already_seen:
>         n = count_item(grub, item)
>         print item, "appears in list", n, "times."
>         already_seen.append(item)
> 
> Notice that rather than *deleting* from a copy of the original list,
> we *add* to a new list that started off empty.
> 
> Here is another way:




More information about the Python-list mailing list