List Manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman

    #1

    List Manipulation

    I would appreciate it if somebody could tell me where I went wrong in
    the following snipet:

    When I run I get no result

    cnt = 0
    p=[]
    reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
    quotechar="'", delimiter='\t')
    for line in reader:
    if cnt 6:
    break
    for col in line:
    p[:0].append(str(col ))
    cnt = cnt + 1

    print p

    when I change it to the following, I get rows back

    cnt = 0
    p=[]
    reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
    quotechar="'", delimiter='\t')
    for line in reader:
    if cnt 6:
    break
    for col in line:
    print col
    cnt = cnt + 1

    print p


    Thanks in advance

  • Mike Kent

    #2
    Re: List Manipulation

    Roman wrote:
    I would appreciate it if somebody could tell me where I went wrong in
    the following snipet:
    >
    When I run I get no result
    >
    cnt = 0
    p=[]
    reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
    quotechar="'", delimiter='\t')
    for line in reader:
    if cnt 6:
    break
    for col in line:
    p[:0].append(str(col ))
    cnt = cnt + 1
    >
    print p
    I'm having trouble deciding what you *intend* this program to do. It
    looks like you want to take the first 7 lines of the input file, and
    append all the data elements in those lines into one long list. If
    that's what you want to do, then you are almost there, although you
    could have written it better. If that's NOT what you want to do...
    well, there are tutorials.

    The problem with this code is in the line 'p[:0].append(str(col )).
    Given a list p, p[:0] will give you the part of p *prior* to element 0.
    Since there is never anything in a list prior to element 0, you will
    always get an empty list back.

    I assume this is not what you intended. But just *what* do you intend?
    I sure can't say.

    Comment

    • Iain King

      #3
      Re: List Manipulation


      Roman wrote:
      I would appreciate it if somebody could tell me where I went wrong in
      the following snipet:
      >
      When I run I get no result
      >
      cnt = 0
      p=[]
      reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
      quotechar="'", delimiter='\t')
      for line in reader:
      if cnt 6:
      break
      for col in line:
      p[:0].append(str(col ))
      What are you trying to do here? p[:0] returns a new list, of all the
      elements in p up to element 0 (which is of course the empty list),
      which is then appended to, but is not stored anywhere. If you want to
      insert str(col) then use p.insert


      Iain

      Comment

      • Laszlo Nagy

        #4
        Re: List Manipulation

        Roman írta:
        I would appreciate it if somebody could tell me where I went wrong in
        the following snipet:
        >
        When I run I get no result
        >
        cnt = 0
        p=[]
        reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
        quotechar="'", delimiter='\t')
        for line in reader:
        if cnt 6:
        break
        for col in line:
        p[:0].append(str(col ))
        >
        You are appending to a slice. In that case, p[:0] creates a new list
        object. You are not appending to p but to a new object (created from a
        slice).
        If you need to insert an item at the begining of a list, use the insert
        method instead.
        >>l = [2,3,4]
        >>l.insert(1, 0)
        >>l
        [2, 0, 3, 4]


        Best,

        Laszlo

        Comment

        • Steven D'Aprano

          #5
          Re: List Manipulation

          On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:
          I would appreciate it if somebody could tell me where I went wrong in
          the following snipet:
          >
          When I run I get no result
          What do you mean? Does it print None?
          cnt = 0
          p=[]
          reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
          quotechar="'", delimiter='\t')
          for line in reader:
          if cnt 6:
          break
          That's a very unPythonic way of doing the job. The usual way of doing
          this would be something like this:

          for line in reader[:7]:
          # no need for the "if cnt 6: break" clause now

          for col in line:
          p[:0].append(str(col ))
          p[:0] creates a new list, which has a string appended to it, and is then
          thrown away. What are you trying to do?

          If you are trying to insert the new entry at the beginning of the list,
          you probably want this:

          p.insert(0, str(col))



          Comment

          • Roman

            #6
            Re: List Manipulation

            Thanks for your help

            My intention is to create matrix based on parsed csv file. So, I would
            like to have a list of columns (which are also lists).

            I have made the following changes and it still doesn't work.


            cnt = 0
            p=[[], [], [], [], [], [], [], [], [], [], []]
            reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
            quotechar="'", delimiter='\t')
            for line in reader:
            if cnt 6:
            break
            j = 0
            for col in line:
            p[j].append(col)
            j=j+1
            cnt = cnt + 1

            print p

            Iain King wrote:
            Roman wrote:
            I would appreciate it if somebody could tell me where I went wrong in
            the following snipet:

            When I run I get no result

            cnt = 0
            p=[]
            reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
            quotechar="'", delimiter='\t')
            for line in reader:
            if cnt 6:
            break
            for col in line:
            p[:0].append(str(col ))
            >
            What are you trying to do here? p[:0] returns a new list, of all the
            elements in p up to element 0 (which is of course the empty list),
            which is then appended to, but is not stored anywhere. If you want to
            insert str(col) then use p.insert
            >
            >
            Iain

            Comment

            • Sibylle Koczian

              #7
              Re: List Manipulation

              Roman schrieb:
              I would appreciate it if somebody could tell me where I went wrong in
              the following snipet:
              >
              When I run I get no result
              >
              cnt = 0
              p=[]
              reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
              quotechar="'", delimiter='\t')
              for line in reader:
              if cnt 6:
              break
              for col in line:
              p[:0].append(str(col ))
              This is wrong. I'm not absolutely certain _what_ it does, but it doesn't
              append anything to list p. p[:0] is an empty copy of p, you are
              appending to this empty copy, not to p. What's wrong with
              p.append(str(co l))?
              when I change it to the following, I get rows back
              >
              cnt = 0
              p=[]
              reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
              quotechar="'", delimiter='\t')
              for line in reader:
              if cnt 6:
              break
              for col in line:
              print col
              cnt = cnt + 1
              >
              print p
              >
              Here you print every single cell, but p doesn't change.

              HTH
              Koczian

              --
              Dr. Sibylle Koczian
              Universitaetsbi bliothek, Abt. Naturwiss.
              D-86135 Augsburg
              e-mail : Sibylle.Koczian @Bibliothek.Uni-Augsburg.DE

              Comment

              • Mike Kent

                #8
                Re: List Manipulation

                Roman wrote:
                Thanks for your help
                >
                My intention is to create matrix based on parsed csv file. So, I would
                like to have a list of columns (which are also lists).
                >
                I have made the following changes and it still doesn't work.
                >
                >
                cnt = 0
                p=[[], [], [], [], [], [], [], [], [], [], []]
                reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
                quotechar="'", delimiter='\t')
                for line in reader:
                if cnt 6:
                break
                j = 0
                for col in line:
                p[j].append(col)
                j=j+1
                cnt = cnt + 1
                >
                print p
                p[j] does not give you a reference to an element inside p. It gives
                you a new sublist containing one element from p. You then append a
                column to that sublist. Then, since you do nothing more with that
                sublist, YOU THROW IT AWAY.

                Try doing:

                p[j] = p[j].append(col)

                However, this will still result in inefficient code. Since every line
                you read in via the csv reader is already a list, try this (untested)
                instead:

                reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
                quotechar="'", delimiter='\t')
                p = [ line for line in reader[:7] ]

                Comment

                • Diez B. Roggisch

                  #9
                  Re: List Manipulation

                  p[j] does not give you a reference to an element inside p. It gives
                  you a new sublist containing one element from p. You then append a
                  column to that sublist. Then, since you do nothing more with that
                  sublist, YOU THROW IT AWAY.
                  Not correct.

                  p = [[]]
                  p[0].append(1)
                  print p

                  yields

                  [[1]]

                  p[0] _gives_ you a reference to an object. If it is mutable (list are) and
                  append mutates it (it does), the code is perfectly alright.

                  I don't know what is "not working" for the OP, but actually his code works
                  if one replaces the csv-reading with a generated list:

                  cnt = 0
                  p=[[], [], [], [], [], [], [], [], [], [], []]
                  reader = [["column_%i" % c for c in xrange(5)] for l in xrange(7)]
                  for line in reader:
                  if cnt 6:
                  break
                  j = 0
                  for col in line:
                  p[j].append(col)
                  j=j+1
                  cnt = cnt + 1
                  print p


                  You are right of course that it is the most unpythonic way imaginabe to do
                  it. But it works.

                  Diez

                  Comment

                  • Roman

                    #10
                    Re: List Manipulation

                    Nothing got printed.

                    Could you tell me what would be pythonic version of what I am trying to
                    do?


                    Diez B. Roggisch wrote:
                    p[j] does not give you a reference to an element inside p. It gives
                    you a new sublist containing one element from p. You then append a
                    column to that sublist. Then, since you do nothing more with that
                    sublist, YOU THROW IT AWAY.
                    >
                    Not correct.
                    >
                    p = [[]]
                    p[0].append(1)
                    print p
                    >
                    yields
                    >
                    [[1]]
                    >
                    p[0] _gives_ you a reference to an object. If it is mutable (list are) and
                    append mutates it (it does), the code is perfectly alright.
                    >
                    I don't know what is "not working" for the OP, but actually his code works
                    if one replaces the csv-reading with a generated list:
                    >
                    cnt = 0
                    p=[[], [], [], [], [], [], [], [], [], [], []]
                    reader = [["column_%i" % c for c in xrange(5)] for l in xrange(7)]
                    for line in reader:
                    if cnt 6:
                    break
                    j = 0
                    for col in line:
                    p[j].append(col)
                    j=j+1
                    cnt = cnt + 1
                    print p
                    >
                    >
                    You are right of course that it is the most unpythonic way imaginabe to do
                    it. But it works.
                    >
                    Diez

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: List Manipulation

                      Roman wrote:
                      (please dont top-post - corrected)
                      >
                      Iain King wrote:
                      >
                      >>Roman wrote:
                      >>
                      >>>I would appreciate it if somebody could tell me where I went wrong in
                      >>>the following snipet:
                      >>>
                      (snip)
                      >>What are you trying to do here? p[:0] returns a new list, of all the
                      >>elements in p up to element 0 (which is of course the empty list),
                      >>which is then appended to, but is not stored anywhere. If you want to
                      >>insert str(col) then use p.insert
                      >>
                      >>
                      >>Iain
                      >
                      >
                      Thanks for your help
                      >
                      My intention is to create matrix based on parsed csv file. So, I
                      would like to have a list of columns (which are also lists).
                      csv = [
                      ['L0C0', 'L0C1', 'L0C2'],
                      ['L1C0', 'L1C1', 'L1C2'],
                      ['L2C0', 'L2C1', 'L2C2'],
                      ]

                      matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]

                      for numline, line in enumerate(csv):
                      for numcol, col in enumerate(line) :
                      matrix[numcol][numline] = col

                      assert matrix == [
                      ['L0C0', 'L1C0', 'L2C0'],
                      ['L0C1', 'L1C1', 'L2C1'],
                      ['L0C2', 'L1C2', 'L2C2']
                      ]

                      NB : There are probably more elegant solutions.
                      I have made the following changes and it still doesn't work.
                      "doesn't work" is the worst possible description of a problem...

                      (snip)

                      --
                      bruno desthuilliers
                      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                      p in 'onurb@xiludom. gro'.split('@')])"

                      Comment

                      • Roman

                        #12
                        Re: List Manipulation

                        I am getting

                        TypeError: unsubscriptable object

                        when specifying

                        for line in reader[:7]:


                        Steven D'Aprano wrote:
                        On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:
                        >
                        I would appreciate it if somebody could tell me where I went wrong in
                        the following snipet:

                        When I run I get no result
                        >
                        What do you mean? Does it print None?
                        >
                        cnt = 0
                        p=[]
                        reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
                        quotechar="'", delimiter='\t')
                        for line in reader:
                        if cnt 6:
                        break
                        >
                        That's a very unPythonic way of doing the job. The usual way of doing
                        this would be something like this:
                        >
                        for line in reader[:7]:
                        # no need for the "if cnt 6: break" clause now
                        >
                        >
                        for col in line:
                        p[:0].append(str(col ))
                        >
                        p[:0] creates a new list, which has a string appended to it, and is then
                        thrown away. What are you trying to do?
                        >
                        If you are trying to insert the new entry at the beginning of the list,
                        you probably want this:
                        >
                        p.insert(0, str(col))

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: List Manipulation

                          Roman wrote:
                          (please dont top-post - corrected)
                          >
                          Steven D'Aprano wrote:
                          >
                          >>On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:
                          >>
                          >>
                          (snip)
                          >>
                          >>>cnt = 0
                          >>>p=[]
                          >>>reader = csv.reader(file ("f:\webserver\ inp.txt"), dialect="excel" ,
                          >> quotechar="'", delimiter='\t')
                          >>>for line in reader:
                          >> if cnt 6:
                          >> break
                          >>
                          >>That's a very unPythonic way of doing the job. The usual way of doing
                          >>this would be something like this:
                          >>
                          >>for line in reader[:7]:
                          > # no need for the "if cnt 6: break" clause now
                          >>
                          I am getting
                          >
                          TypeError: unsubscriptable object
                          >
                          when specifying
                          >
                          for line in reader[:7]:
                          >
                          Should have been :

                          for line in list(reader)[:7]:
                          # code here


                          --
                          bruno desthuilliers
                          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                          p in 'onurb@xiludom. gro'.split('@')])"

                          Comment

                          • Roman

                            #14
                            Re: List Manipulation

                            Thanks for spending so much time with me. I had since made the
                            following change.

                            matrix = [[[] for l in range(len(list( reader)[:10]))] for c in
                            range(len(list( reader)[7]))]
                            for numline, line in enumerate(reade r):
                            for numcol, col in enumerate(line) :
                            matrix[numcol][numline] = col

                            print matrix

                            I don't get mistakes anymore. However, still nothing gets printed
                            Bruno Desthuilliers wrote:
                            Roman wrote:
                            (please dont top-post - corrected)

                            Iain King wrote:
                            >Roman wrote:
                            >
                            >>I would appreciate it if somebody could tell me where I went wrong in
                            >>the following snipet:
                            >>
                            (snip)
                            >
                            >What are you trying to do here? p[:0] returns a new list, of all the
                            >elements in p up to element 0 (which is of course the empty list),
                            >which is then appended to, but is not stored anywhere. If you want to
                            >insert str(col) then use p.insert
                            >
                            >
                            >Iain

                            Thanks for your help

                            My intention is to create matrix based on parsed csv file. So, I
                            would like to have a list of columns (which are also lists).
                            >
                            csv = [
                            ['L0C0', 'L0C1', 'L0C2'],
                            ['L1C0', 'L1C1', 'L1C2'],
                            ['L2C0', 'L2C1', 'L2C2'],
                            ]
                            >
                            matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]
                            >
                            for numline, line in enumerate(csv):
                            for numcol, col in enumerate(line) :
                            matrix[numcol][numline] = col
                            >
                            assert matrix == [
                            ['L0C0', 'L1C0', 'L2C0'],
                            ['L0C1', 'L1C1', 'L2C1'],
                            ['L0C2', 'L1C2', 'L2C2']
                            ]
                            >
                            NB : There are probably more elegant solutions.
                            >
                            I have made the following changes and it still doesn't work.
                            >
                            "doesn't work" is the worst possible description of a problem...
                            >
                            (snip)
                            >
                            --
                            bruno desthuilliers
                            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                            p in 'onurb@xiludom. gro'.split('@')])"

                            Comment

                            • Bruno Desthuilliers

                              #15
                              Re: List Manipulation

                              Roman a écrit :

                              <ot>
                              Roman, please stop top-posting and learn to quote.
                              </ot>
                              Bruno Desthuilliers wrote:
                              >
                              >>Roman wrote:
                              >>(please dont top-post - corrected)
                              >>
                              >>>
                              >>>My intention is to create matrix based on parsed csv file. So, I
                              >>>would like to have a list of columns (which are also lists).
                              >>
                              >>csv = [
                              > ['L0C0', 'L0C1', 'L0C2'],
                              > ['L1C0', 'L1C1', 'L1C2'],
                              > ['L2C0', 'L2C1', 'L2C2'],
                              >]
                              >>
                              >>matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]
                              >>
                              >>for numline, line in enumerate(csv):
                              > for numcol, col in enumerate(line) :
                              > matrix[numcol][numline] = col
                              >>
                              >>assert matrix == [
                              > ['L0C0', 'L1C0', 'L2C0'],
                              > ['L0C1', 'L1C1', 'L2C1'],
                              > ['L0C2', 'L1C2', 'L2C2']
                              >]
                              >>
                              Thanks for spending so much time with me. I had since made the
                              following change.
                              >
                              matrix = [[[] for l in range(len(list( reader)[:10]))] for c in
                              range(len(list( reader)[7]))]
                              Technically; this won't work. The first call to list(reader) will
                              consume reader.

                              It's also somewhat dumb (len(list(reade r)[:10]) is always 10) and
                              inefficient (you're calling list(reader) twice, when you could call it
                              just once).

                              Instead of trying anything at random, read the fine manual and try to
                              understand the example I gave you.
                              for numline, line in enumerate(reade r):
                              for numcol, col in enumerate(line) :
                              matrix[numcol][numline] = col
                              >
                              print matrix
                              >
                              I don't get mistakes anymore. However, still nothing gets printed
                              If you don't print anything, nothing will be printed.

                              Comment

                              Working...