假设您有以下元组的元组:
test = ((1, '2'), (3, '4'), ('5', '6'))
如果我想提取每个元组的第二个元素,我会这样做:
secondonly = tuple([x[-1] for x in test])
我还看到过:
secondonly = tuple([x for word, x in test])
<---- ** <--- 这是如何工作的?
有人能解释一下**这行代码是如何得到正确结果的吗?谢谢。
secondonly = tuple([x for _, x in test])
- Nils Wernertuple(x for word, x in test)
与tuple([x for word, x in test])
相比,效果相同甚至更好。 - user2555451