开发者

simple python list comprehension question

i am trying to select the elements of a list without the very first element. the following code works but it kinda look ugly to me

[s[i] for i in range(len(s)) if i>0]

is the开发者_开发技巧re a better way to write it? thanks


Use the slicing notation:

s[1:]

Alternatively, you can avoid copying the list thus:

itertools.islice(s, 1, None)

The result isn't a list — it doesn't support random access, for instance — but you can pass it to anything that accepts an iterator.


Wouldn't s[1:] be correct?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜