list insertion question

E

eight02645999

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
 
M

Michael Hoffman

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(item)
if item == "a":
output.append("aword")
['a', 'aword', 'b', 'c', 'dword', 'd', 'a', 'aword', 'b', 'e', 'dword', 'd']
 
J

Jun.Jin.act+group.python

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(item)
if item == "a":
output.append("aword")
['a', 'aword', 'b', 'c', 'dword', 'd', 'a', 'aword', 'b', 'e', 'dword', 'd']

Infinite loop for me too! ^_^, should think of it b4 pressing F5.
 
A

attn.steven.kuo

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(enumerate(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(ridx+1, 'aword')
elif data[ridx] == 'd':
data.insert(ridx, 'dword')

print data
 
P

Paul Rubin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top