Here I am again, same old arguments

CJ cj at nowhere.com
Sun Oct 9 09:02:52 CEST 2005


 
   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.

   Well, for some reason, the FOR loop is altering two of my lists. Using 
PRINT magic, I was able to narrow down the lines that were causing it. 
But the question remains: Why is it doing this? I'm sure there's a simple 
answer that I just overlooked in the manual or something.

So without further ado, the code:


#setup variables
grub=[3,25,3,5,3,"a","a","BOB",3,3,45,36,26,25,"a",3,3,3,"bob","BOB",67]
grubrpt=grub
cntro=0
cntrt=0
rpt=0
skipped=0

#set up for variable length of grub
ttllen=len(grub)-1
print "The heck is this for loop doing?"
for point in range(0,ttllen,1):
    print "Here's Grub=",grub
    print "And grubrpt=",grubrpt
    grubrpt[point]="blk"

#Makes sure that there are not multiple prints.
def alrdy_dn(grub,grubrpt): 
    if grub[cntro] in grubrpt:
        return grubrpt
    else:
        print grub[cntro],"appears in list",rpt,"times."
        grubrpt[grubrpt.index("blk")]=grub[cntro]
        return grubrpt

#removes display of variables not repeated
def no_rpts(skipped,grubrpt): 
    if rpt==0:
        skipped=skipped+1
    else:
        grubrpt=alrdy_dn(grub,grubrpt)
    return skipped

#Main body of code
print "The List is:",grub

while cntro<>len(grub)-1:
    if grub[cntro]==grub[cntrt]:
        rpt=rpt+1
        cntrt=cntrt+1
    else:
        cntrt=cntrt+1
    if cntrt==len(grub):
        skipped=no_rpts(skipped,grubrpt)
        cntro=cntro+1
        cntrt=0
        rpt=-1
        
print skipped,"list elements are unique."


And the award winning Output:

The heck is this for loop doing?
Here's Grub= [3, 25, 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 25, 'a', 
3, 3, 3, 'bob', 'BOB', 67]
And grubrpt= [3, 25, 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 25, 'a', 
3, 3, 3, 'bob', 'BOB', 67]
Here's Grub= ['blk', 25, 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 25, 
'a', 3, 3, 3, 'bob', 'BOB', 67]
And grubrpt= ['blk', 25, 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 25, 
'a', 3, 3, 3, 'bob', 'BOB', 67]
Here's Grub= ['blk', 'blk', 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 
25, 'a', 3, 3, 3, 'bob', 'BOB', 67]
And grubrpt= ['blk', 'blk', 3, 5, 3, 'a', 'a', 'BOB', 3, 3, 45, 36, 26, 
25, 'a', 3, 3, 3, 'bob', 'BOB', 67]

   It goes on like that, I'm not going to put all of it here for obvious 
reasons. But, if I take out the whole for loop and the lines relating to 
it and statically assign grubrpt as ["blk","blk"...] then the program 
runs wonderfully.


   From what I understand, you can never have too many functions, so I 
tried to make the grub "blk" a function and got the same result. I'm 
still nailing them down, so if my functions look a little weird you know 
why. Also, I do realize that there is an easier way to do this, I just 
created a little project for myself to learn the basics of the language.

        Thanks for all the help!
-CJ




More information about the Python-list mailing list