如何交换不包含相同项目数的两个元组的值?

Zeh*_*enE 1 python tuples python-2.7

我有两个元组:

tup_1 = ('hello', 'world')
tup_2 = (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

在同一行上打印,我得到:

('hello', 'world') (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

我想要的交换价值tup_1tup_2,这样,当我现在把它们打印在同一行,我得到:

(1, 2, 3) ('hello', 'world')
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

the*_*eye 5

您可以像任何其他对象一样交换元组,

>>> print tup_1, tup_2
('hello', 'world') (1, 2, 3)
>>> tup_1, tup_2 = tup_2, tup_1
>>> print tup_1, tup_2
(1, 2, 3) ('hello', 'world')
Run Code Online (Sandbox Code Playgroud)