我想对列表中的第一项进行不同的操作。最符合Python风格的方法是什么?
for item in list:
# only if its the first item, do something
# otherwise do something else
以下是根据 Pythonic 程度降序排列的一些选择:
for index, item in enumerate(lst): # note: don't use list
if not index: # or if index == 0:
# first item
else:
# other items
或者:
first = True
for item in lst:
if first:
first = False
# first item
else:
# other items
或者:
for index in range(len(lst)):
item = lst[i]
if not index:
# first item
else:
# other items
enumerate
相比其他选项的一个优点是,无论被迭代的对象是否支持索引,都不会产生影响。 - DSMif not index
。if index==0
更加明确。 - Adam Smithif not index:
示例。 - abarnertnext
函数,每次调用比每个if
语句花费的时间长几个数量级,因此比例没有改变。 - abarnert您可以使用iter()在列表上创建迭代器,然后调用next()以获取第一个值,然后循环遍历其余部分。我发现这是一种非常优雅的处理首行为标题,其余都是数据的文件的方式,即:
list_iterator = iter(lst)
# consume the first item
first_item = next(list_iterator)
# now loop on the tail
for item in list_iterator:
print(item)
do_something_with_first_item(lst[0])
for item in lst[1:]:
do_something_else(item)
或者:
is_first_item = True
for item in lst:
if is_first_item:
do_something_with_first_item(item)
is_first_item = False
else:
do_something_else(item)
不要使用 list
作为变量名,因为这会掩盖内置函数 list()
。
在 jonrsharpe 的答案中,基于 enumerate
的解决方案更优。您应该使用那个方案。
使用一个标志,在处理第一项后进行更改。例如:
first = True
for item in my_list:
if first:
# Processing unique to the first item
first = False
else:
# Processing unique to other items
# Shared processing
first = my_list.pop(0)
# Process first
for item in my_list:
# Process item
# Add the first item back to the list at the beginning
my_list.insert(0, first)
.pop()
。老实说,这是最好、最干净的解决方案。 - mklauberpop()
方法,例如元组,因此这不是一种通用解决方案。 - martineaujonrsharpe的第一个版本使用enumerate
非常简洁明了,适用于所有可迭代对象:
for index, item in enumerate(lst):
if not index:
do_something_with_first_item(item)
else:
do_something_else(item)
senshin的第一种解决方案是使用lst[0]
和 lst[1:]
,非常简单,但只适用于序列:
do_something_with_first_item(lst[0])
for item in lst[1:]:
do_something_else(item)
通过直接使用迭代器,您可以兼顾两者:
it = iter(lst)
do_something_with_first_item(next(it))
for item in it:
do_something_else(item)
但是,尽管它融合了两种最好的方式,实际上并不像看起来那么简单。只有当您知道您拥有一个迭代器(这样您就可以跳过第一行),或者您需要使用itertools
和genexpr等类似的东西时才值得这样做(所以您已经打算写第一行)。在其他情况下是否值得这样做更多的是一个风格问题。
it
是一个迭代器,iter(it)
保证返回it
——事实上,这就是迭代器的定义的一部分。关键在于,如果你知道它是一个迭代器,你可以跳过第一行,使这成为最简单的解决方案;如果你有一个序列,或者不知道你有什么类型的可迭代对象,你不能跳过第一行,所以它可能不是最简单的。 - abarnert