Return a callable object that fetches item from its operand using the operand’s __getitem__() method. If multiple items are specified, returns a tuple of lookup values.
示例:
>>> from operator import itemgetter
>>> pick = itemgetter(0, 2)
>>> pick("Jack and Jill went up the hill".split())
('Jack', 'Jill')
或作为一行(不含进口):
>>> user1, user2 = itemgetter(0, 2)("Jack and Jill went up the hill".split())
切片支持步骤参数
但是,虽然用Python编写时髦的单行程序是可能的,但它肯定不是进行这种练习的最佳语言。
你可以这样做
其中
_
表示我们不关心值,并且split(" ", 3)
将字符串分成4段。如果你想变得花哨,你可以使用^{} :
示例:
或作为一行(不含进口):
相关问题 更多 >
编程相关推荐