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.
假设我有
x = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
我怎么去
x = ((1, 2), (4, 5), (7, 8))
?
我想出的唯一方法是使用列表理解然后转换回元组:
x = tuple([n[1:len(n)] for n in x])
但我觉得这是一种丑陋的做法......
In [1]: x = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) In [2]: tuple(a[:-1] for a in x)
您可以使用生成器表达式而不是列表推导(它们几乎是一样的):
x = tuple(n[1:] for n in x)
请注意,这不会给您上面的内容。如果你想切断你真的应该这样做:
x = tuple(n[:-1] for n in x)