Results 1 to 3 of 3

Thread: Problems with Python

  1. #1
    Join Date
    Nov 2006
    Location
    Spain
    Beans
    175
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Problems with Python

    So I was writing my 1st prime finder sieve like this:

    Code:
    hasta=10
    numeros=range(2,hasta+1)
    lol=numeros
    print numeros
    for i in numeros:
        for o in numeros:
          quitar=o*i
          
          if quitar   in numeros:
            lol.remove(quitar)    
    
    print lol
    print numeros
    Basically, the resulting primes should go to "lol", but the weird thing is that both "print lol" and "print numeros" return the same, even though I'm not doing anything to numeros but reading the i and o vars. What is that happening?

  2. #2
    Join Date
    Jul 2008
    Beans
    565

    Re: Problems with Python

    That's because when you assign one list to another, you're copying by reference. You end up with two different names that both refer to the same list. Observe:

    Code:
    In [69]: a = range(20)
    
    In [70]: b = a
    
    In [71]: a is b      <----------- a and b are literally the same object
    Out[71]: True
    
    In [72]: a[4]="spam" <----------- now I assign the value "spam" to a[4]
    
    In [73]: b           <----------- b now reflects the change
    Out[73]: [0, 1, 2, 3, 'spam', 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    There are many different ways to copy by value. Here's a few:
    Code:
    b = list(a)
       OR
    b = a[:]
       OR
    b = [ i for i in a ]
       OR
    from copy import copy
    b = copy(a)
       OR
    from copy import deepcopy
    b = deepcopy(a)
    Last edited by eightmillion; January 25th, 2009 at 11:01 PM.
    Code:
    ruby -ne '$_.gsub(/<[^>]*>|\([^)]*\)|\[[^\]]*\]/,"").each_char{|i|STDOUT.flush.print(i);sleep(0.03)}if/(<\/li>|<ul>)<li>/' <(wget -qO- is.gd/e3EGx)

  3. #3
    Join Date
    Nov 2006
    Location
    Spain
    Beans
    175
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Problems with Python

    Thanks!
    This is what i did. Is now fully operative.
    PHP Code:
    from copy import copy

    hasta
    =1000
    numeros
    =range(2,hasta+1)
    lol=copy(numeros)



            
    def calc():
        for 
    x in lol:
            if (
    numeros[-1]**0.5)< x:
              
             if 
    x%2==0:
                if 
    x!=2:
                    
                    break
                
    lol.remove(x)
        for 
    i in lol:
            
            for 
    o in numeros:
              
              
    quitar=o*i
             
              
              
    if quitar   in lol:
               
                
    lol.remove(quitar)
                
                
                
                
    calc()
    print 
    lol 
    Last edited by artir; January 26th, 2009 at 04:45 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •