I need to do some special operation for the last element in a list. Is there any better way than this?
我需要对列表中的最后一个元素进行一些特殊操作。还有比这更好的方法吗?
array = [1,2,3,4,5] for i, val in enumerate(array): if (i+1) == len(array): // Process for the last element else: // Process for the other element
5 个解决方案
#1
48
for item in list[:-1]:
print "Not last: ", item
print "Last: ", list[-1]
If you don't want to make a copy of list, you can make a simple generator:
如果您不想复制列表,可以创建一个简单的生成器:
# itr is short for "iterable" and can be any sequence, iterator, or generator
def notlast(itr):
itr = iter(itr) # ensure we have an iterator
prev = itr.next()
for item in itr:
yield prev
prev = item
# lst is short for "list" and does not shadow the built-in list()
# 'L' is also commonly used for a random list name
lst = range(4)
for x in notlast(lst):
print "Not last: ", x
print "Last: ", lst[-1]
Another definition for notlast:
notlast的另一个定义:
import itertools
notlast = lambda lst:itertools.islice(lst, 0, len(lst)-1)
#2
28
If your sequence isn't terribly long then you can just slice it:
如果您的序列不是非常长,那么您可以将其切片:
for val in array[:-1]:
do_something(val)
else:
do_something_else(array[-1])
#3
6
using itertools
使用itertools
>>> from itertools import repeat, chain,izip
>>> for val,special in izip(array, chain(repeat(False,len(array)-1),[True])):
... print val, special
...
1 False
2 False
3 False
4 False
5 True
Version of liori's answer to work on any iterable (doesn't require len()
or slicing)
liori对任何可迭代的工作答案的版本(不需要len()或切片)
def last_flagged(seq):
seq = iter(seq)
a = next(seq)
for b in seq:
yield a, False
a = b
yield a, True
mylist = [1,2,3,4,5]
for item,is_last in last_flagged(mylist):
if is_last:
print "Last: ", item
else:
print "Not last: ", item
#4
-1
Simple way with an if condition:
if条件的简单方法:
for item in list:
print "Not last: ", item
if list.index(item) == len(list)-1:
print "Last: ", item
#5
-3
for i in items:
if i == items[-1]:
print 'The last item is: '+i
#1
48
for item in list[:-1]:
print "Not last: ", item
print "Last: ", list[-1]
If you don't want to make a copy of list, you can make a simple generator:
如果您不想复制列表,可以创建一个简单的生成器:
# itr is short for "iterable" and can be any sequence, iterator, or generator
def notlast(itr):
itr = iter(itr) # ensure we have an iterator
prev = itr.next()
for item in itr:
yield prev
prev = item
# lst is short for "list" and does not shadow the built-in list()
# 'L' is also commonly used for a random list name
lst = range(4)
for x in notlast(lst):
print "Not last: ", x
print "Last: ", lst[-1]
Another definition for notlast:
notlast的另一个定义:
import itertools
notlast = lambda lst:itertools.islice(lst, 0, len(lst)-1)
#2
28
If your sequence isn't terribly long then you can just slice it:
如果您的序列不是非常长,那么您可以将其切片:
for val in array[:-1]:
do_something(val)
else:
do_something_else(array[-1])
#3
6
using itertools
使用itertools
>>> from itertools import repeat, chain,izip
>>> for val,special in izip(array, chain(repeat(False,len(array)-1),[True])):
... print val, special
...
1 False
2 False
3 False
4 False
5 True
Version of liori's answer to work on any iterable (doesn't require len()
or slicing)
liori对任何可迭代的工作答案的版本(不需要len()或切片)
def last_flagged(seq):
seq = iter(seq)
a = next(seq)
for b in seq:
yield a, False
a = b
yield a, True
mylist = [1,2,3,4,5]
for item,is_last in last_flagged(mylist):
if is_last:
print "Last: ", item
else:
print "Not last: ", item
#4
-1
Simple way with an if condition:
if条件的简单方法:
for item in list:
print "Not last: ", item
if list.index(item) == len(list)-1:
print "Last: ", item
#5
-3
for i in items:
if i == items[-1]:
print 'The last item is: '+i