moving items in a list
Manuel M. Garcia
mgarcia at cole-switches.com
Tue Jan 7 21:04:39 EST 2003
def move_up(l, m):
for i in range(1, len(l)):
if l[i] in m:
l[i-1],l[i] = l[i],l[i-1]
def move_down(l, m):
for i in range(len(l) - 2, 0, -1):
if l[i] in m:
l[i],l[i+1] = l[i+1],l[i]
Manuel
On Tue, 7 Jan 2003 15:30:07 -0600, "Mark McEahern" <mark at mceahern.com>
wrote:
(edit)
>def move_up(l, m):
> """Move items in m found in l up one position."""
> for i in range(len(l)):
> if i == 0:
> continue
> item = l[i]
> if item not in m:
> continue
> j = i - 1
> swap_with = l[j]
> l[i] = swap_with
> l[j] = item
>
>def move_down(l, m):
> """Move items in m found in l down one position."""
> for i in range(len(l) - 1, 0, -1):
> if i == len(l) - 1:
> continue
> item = l[i]
> if item not in m:
> continue
> j = i + 1
> swap_with = l[j]
> l[i] = swap_with
> l[j] = item
More information about the Python-list
mailing list