what is wrong with this line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jenya56
    New Member
    • Nov 2009
    • 14

    what is wrong with this line

    Code:
    list[1].extend(data_ncdc[il][3])
    it gives me an error that 'str' object has no attribute 'extend'. but I also tried
    Code:
    list[1].extend(float(data_ncdc[il][3]))
    Anybody? THANKS
    Last edited by bvdet; Dec 1 '09, 09:36 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should not use "list" as an identifier. The assignment masks the built-in Python function "list()".

    You are receiving the error because "list[1]" is a str object, which has no append() method. Use print statements in your code prior to the error to see what the object is.

    Comment

    • jenya56
      New Member
      • Nov 2009
      • 14

      #3
      Yes, I did print all of these.....
      However, in my program I have:
      Code:
       if coord_ind == None:
          ncdc_station.append(data_ncdc[il][3])
       else:
          ncdc_station[coord_ind[0]].extend(data_ncdc[il][3])
      In this program append works just fine but then I go to extend it does not. I thought that if extend does not work, append will not either?
      Could you please help me to correct this problem. THANKS
      Last edited by bvdet; Dec 1 '09, 10:54 PM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Please use code tags when posting code.

        append() and extend() are list methods and will fail on str objects. It's hard for me to know what is going on with your data.
        Code:
        >>> a = [1,2,3]
        >>> a.extend('a')
        >>> a
        [1, 2, 3, 'a']
        >>> a.extend([1,2,3])
        >>> a
        [1, 2, 3, 'a', 1, 2, 3]
        >>> 's'.extend([3,4,5])
        Traceback (most recent call last):
          File "<interactive input>", line 1, in ?
        AttributeError: 'str' object has no attribute 'extend'
        >>> 's'.append(1)
        Traceback (most recent call last):
          File "<interactive input>", line 1, in ?
        AttributeError: 'str' object has no attribute 'append'
        >>>

        Comment

        • jenya56
          New Member
          • Nov 2009
          • 14

          #5
          well, I have array a=['0','1','2','3']. I need now to do the following val1='9', then set
          a=['0',['1','9'],'2','3']. Is there a way to do it? Thanks.

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Code:
            In [5]: a=['0','1','2','3']
            
            In [6]: type(a[1])
            Out[6]: <type 'str'>
            
            In [7]: a[1]=list(a[1])
            
            In [8]: a
            Out[8]: ['0', ['1'], '2', '3']
            
            In [9]: a[1].append('9')
            
            In [10]: a
            Out[10]: ['0', ['1', '9'], '2', '3']

            Comment

            • Glenton
              Recognized Expert Contributor
              • Nov 2008
              • 391

              #7
              You just need to be clear about what type a variable is. Appending to a string is not valid.

              Comment

              Working...