Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以,我有一个这样的元组:
a=[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
我想用 100 替换每个元组的最后一个值。所以我可以这样做:
b=[(t[0],t[1],) + (100,) for t in a]
这给了我这个:
[(1, 2, 100), (4, 5, 100), (7, 8, 100)].
什么是捷径?这些元组实际上有 50 个元素吗?
使用元组切片:
[t[:-1] + (100,) for t in a]
这里不需要从单个元素构建一个全新的元组。