list insertion question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eight02645999@yahoo.com

    #1

    list insertion question

    hi
    i have a list (after reading from a file), say
    data = [ 'a','b','c','d' ,'a','b','e','d ']

    I wanted to insert a word after every 'a', and before every 'd'. so i
    use enumerate this list:
    for num,item in enumerate(data) :
    if "a" in item:
    data.insert(num +1,"aword")
    if "d" in item:
    data.insert(num-1,"dword") #this fails
    but the above only inserts after 'a' but not before 'd'. What am i
    doing wrong? is there better way?thanks

  • Michael Hoffman

    #2
    Re: list insertion question

    eight02645999@y ahoo.com wrote:
    hi
    i have a list (after reading from a file), say
    data = [ 'a','b','c','d' ,'a','b','e','d ']
    >
    I wanted to insert a word after every 'a', and before every 'd'. so i
    use enumerate this list:
    for num,item in enumerate(data) :
    if "a" in item:
    data.insert(num +1,"aword")
    if "d" in item:
    data.insert(num-1,"dword") #this fails
    but the above only inserts after 'a' but not before 'd'. What am i
    doing wrong? is there better way?thanks
    If you modify a list while you are iterating over it, you may get
    unexpected results (an infinite loop in this case for me). Also ("a" in
    item) will match "aword" since "a" is a component of it. I imagine you
    mean (item == "a").

    Try this:

    output = []
    for item in data:
    if item == "d":
    output.append(" dword")
    output.append(i tem)
    if item == "a":
    output.append(" aword")
    >>output
    ['a', 'aword', 'b', 'c', 'dword', 'd', 'a', 'aword', 'b', 'e', 'dword', 'd']
    --
    Michael Hoffman

    Comment

    • Jun.Jin.act+group.python@gmail.com

      #3
      Re: list insertion question

      On Apr 17, 9:47 am, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
      eight02645...@y ahoo.com wrote:
      hi
      i have a list (after reading from a file), say
      data = [ 'a','b','c','d' ,'a','b','e','d ']
      >
      I wanted to insert a word after every 'a', and before every 'd'. so i
      use enumerate this list:
      for num,item in enumerate(data) :
      if "a" in item:
      data.insert(num +1,"aword")
      if "d" in item:
      data.insert(num-1,"dword") #this fails
      but the above only inserts after 'a' but not before 'd'. What am i
      doing wrong? is there better way?thanks
      >
      If you modify a list while you are iterating over it, you may get
      unexpected results (an infinite loop in this case for me). Also ("a" in
      item) will match "aword" since "a" is a component of it. I imagine you
      mean (item == "a").
      >
      Try this:
      >
      output = []
      for item in data:
      if item == "d":
      output.append(" dword")
      output.append(i tem)
      if item == "a":
      output.append(" aword")
      >
      >>output
      ['a', 'aword', 'b', 'c', 'dword', 'd', 'a', 'aword', 'b', 'e', 'dword', 'd']
      --
      Michael Hoffman
      Infinite loop for me too! ^_^, should think of it b4 pressing F5.

      Comment

      • attn.steven.kuo@gmail.com

        #4
        Re: list insertion question

        On Apr 16, 6:05 pm, eight02645...@y ahoo.com wrote:
        hi
        i have a list (after reading from a file), say
        data = [ 'a','b','c','d' ,'a','b','e','d ']
        >
        I wanted to insert a word after every 'a', and before every 'd'. so i
        use enumerate this list:
        for num,item in enumerate(data) :
        if "a" in item:
        data.insert(num +1,"aword")
        if "d" in item:
        data.insert(num-1,"dword") #this fails
        but the above only inserts after 'a' but not before 'd'. What am i
        doing wrong? is there better way?thanks

        Traverse the list from highest index
        to lowest index:

        data = [ 'a', 'b', 'c', 'd', 'a', 'b', 'e', 'd' ]

        print data
        for idx, value in reversed(list(e numerate(data)) ):
        if value == 'a':
        data.insert(idx +1, 'aword')
        elif value == 'd':
        data.insert(idx , 'dword')

        print data

        # OR

        last_idx = len(data) - 1

        for idx in range(last_idx+ 1):
        ridx = last_idx - idx
        if data[ridx] == 'a':
        data.insert(rid x+1, 'aword')
        elif data[ridx] == 'd':
        data.insert(rid x, 'dword')

        print data

        --
        Hope this helps,
        Steven

        Comment

        • Paul Rubin

          #5
          Re: list insertion question

          eight02645999@y ahoo.com writes:
          hi
          i have a list (after reading from a file), say
          data = [ 'a','b','c','d' ,'a','b','e','d ']
          >
          I wanted to insert a word after every 'a', and before every 'd'. so i
          use enumerate this list:
          for num,item in enumerate(data) :
          if "a" in item:
          data.insert(num +1,"aword")
          if "d" in item:
          data.insert(num-1,"dword") #this fails
          but the above only inserts after 'a' but not before 'd'. What am i
          doing wrong? is there better way?thanks
          As others have said, you're mutating the list while iterating through
          it, which can give whacked results. Also, even if you operate on a
          copy of the list, that algorithm uses quadratic time because of all
          the insertions into the list. These days I like to write in the style

          def g():
          for w in data:
          if 'd' in w: yield 'dword'
          yield w
          if 'a' in w: yield 'aword'
          data = list(g(data))

          instead of using list.append as someone else suggested. The iterator
          approach is probably a bit slower but can be seen as a bit cleaner,
          depending on your stylistic preferences.

          Comment

          Working...