【问题标题】:How can i switch elements in tuple? [duplicate]如何在元组中切换元素? [复制]
【发布时间】:2017-04-14 00:20:23
【问题描述】:

在元组中切换元素最简单(最短)的方法是什么?

my_tuple= (a,b)

切换后:

new_tuple= (b,a)

【问题讨论】:

    标签: python python-2.7 python-3.x


    【解决方案1】:

    通过反转它:

    >>> my_tuple = ('a', 'b')
    >>> new_tuple = my_tuple[::-1]
    >>> new_tuple
    ('b', 'a')
    

    【讨论】:

      【解决方案2】:
      In [32]: my_tuple= ('a','b')
      
      In [33]: tuple(reversed(my_tuple))
      Out[33]: ('b', 'a')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        • 2020-11-16
        相关资源
        最近更新 更多