【发布时间】:2017-04-14 00:20:23
【问题描述】:
在元组中切换元素最简单(最短)的方法是什么?
my_tuple= (a,b)
切换后:
new_tuple= (b,a)
【问题讨论】:
标签: python python-2.7 python-3.x
在元组中切换元素最简单(最短)的方法是什么?
my_tuple= (a,b)
切换后:
new_tuple= (b,a)
【问题讨论】:
标签: python python-2.7 python-3.x
通过反转它:
>>> my_tuple = ('a', 'b')
>>> new_tuple = my_tuple[::-1]
>>> new_tuple
('b', 'a')
【讨论】:
In [32]: my_tuple= ('a','b')
In [33]: tuple(reversed(my_tuple))
Out[33]: ('b', 'a')
【讨论】: