交换元组中的对象 - python
Swapping objects in tuples - python
我有以下形式的元组:
a[i][j][k]
k ∈ [0,1]
a 由包含在列表 teams 中的对象 T 组成。
我想要的是交换所有对a[i][j][0]和a[i][j][1]的位置。所以:
a[i][j][0], a[i][j][1] = a[i][j][1], a[i][j][0]
因为 a 是一个元组,我知道它是不可变的,这就是它不起作用的原因:
for i in range(0, len(teams)-1):
for j in range(0, len(teams)/2):
a[i][j][0],a[i][j][1] = a[i][j][1],a[i][j][0]
我试过将 a 转换为列表 (L = list(a)),但没有成功。
有人可以帮我提点建议吗?抱歉,如果我的命名法还不完美,这是我关于 SO 的第一个问题 :)
谢谢
假设 a
是 tuple
的 tuple
的 tuple
的 tuple
。
我愿意
- 将其转换为列表列表的列表
- 随心所欲
- 如果需要,将其转换回原始格式
示例:
a = (((1,2),(3,4)),((10,20),(30,40)))
a_as_list = [[list(x) for x in b] for b in a]
print(a_as_list)
# now manipulate elements:
a_as_list[0][0] = [a_as_list[0][0][1],a_as_list[0][0][0]]
a_as_tuple = tuple(tuple(tuple(x) for x in b) for b in a_as_list)
print(a_as_tuple)
结果:
[[[1, 2], [3, 4]], [[10, 20], [30, 40]]]
(((2, 1), (3, 4)), ((10, 20), (30, 40)))
如您所见,元组被转换为列表,进行处理,然后转换回元组
编辑:如果 a
是你不能做的元组列表:
a[i][j][0], a[i][j][1] = a[i][j][1], a[i][j][0]
但您可以重新创建内部元组:
a[i][j] = a[i][j][1],a[i][j][0]
我们可以简单地使用 Python 的 reversed
来做到这一点
a[j][k] = tuple(reversed(a[j][k]))
问题
元组确实是不可变的。 a
可能是一个列表,甚至是列表的列表。所以这段代码:
L = list(a)
不会改变任何东西。问题似乎是 a
是元组列表的列表。 a[i][j]
是一个元组,无法为其分配新值:
>>> t = ('a', 'b')
>>> t[0] = 'b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
可能的解决方案
如果您使用的是 3-D 矩阵,numpy
could probably help you. It supports advanced indexing and slicing:
>>> import numpy as np
>>> table = np.arange(18).reshape(3,3,2)
>>> table
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]]])
>>> table[:, :, [0, 1]] = table[:, :, [1, 0]]
>>> table
array([[[ 1, 0],
[ 3, 2],
[ 5, 4]],
[[ 7, 6],
[ 9, 8],
[11, 10]],
[[13, 12],
[15, 14],
[17, 16]]])
矩阵中的元素不一定是数字,可以是任何对象:
>>> class T(str):
... pass
...
>>> T('test')
'test'
>>> m = np.array([[(T(1), T(2))],[(T(3), T(4))]])
>>> m[:, :, [0, 1]] = m[:, :, [1, 0]]
>>> m
array([[['2', '1']],
[['4', '3']]],
dtype='<U1')
我有以下形式的元组:
a[i][j][k]
k ∈ [0,1] a 由包含在列表 teams 中的对象 T 组成。
我想要的是交换所有对a[i][j][0]和a[i][j][1]的位置。所以:
a[i][j][0], a[i][j][1] = a[i][j][1], a[i][j][0]
因为 a 是一个元组,我知道它是不可变的,这就是它不起作用的原因:
for i in range(0, len(teams)-1):
for j in range(0, len(teams)/2):
a[i][j][0],a[i][j][1] = a[i][j][1],a[i][j][0]
我试过将 a 转换为列表 (L = list(a)),但没有成功。
有人可以帮我提点建议吗?抱歉,如果我的命名法还不完美,这是我关于 SO 的第一个问题 :)
谢谢
假设 a
是 tuple
的 tuple
的 tuple
的 tuple
。
我愿意
- 将其转换为列表列表的列表
- 随心所欲
- 如果需要,将其转换回原始格式
示例:
a = (((1,2),(3,4)),((10,20),(30,40)))
a_as_list = [[list(x) for x in b] for b in a]
print(a_as_list)
# now manipulate elements:
a_as_list[0][0] = [a_as_list[0][0][1],a_as_list[0][0][0]]
a_as_tuple = tuple(tuple(tuple(x) for x in b) for b in a_as_list)
print(a_as_tuple)
结果:
[[[1, 2], [3, 4]], [[10, 20], [30, 40]]]
(((2, 1), (3, 4)), ((10, 20), (30, 40)))
如您所见,元组被转换为列表,进行处理,然后转换回元组
编辑:如果 a
是你不能做的元组列表:
a[i][j][0], a[i][j][1] = a[i][j][1], a[i][j][0]
但您可以重新创建内部元组:
a[i][j] = a[i][j][1],a[i][j][0]
我们可以简单地使用 Python 的 reversed
来做到这一点a[j][k] = tuple(reversed(a[j][k]))
问题
元组确实是不可变的。 a
可能是一个列表,甚至是列表的列表。所以这段代码:
L = list(a)
不会改变任何东西。问题似乎是 a
是元组列表的列表。 a[i][j]
是一个元组,无法为其分配新值:
>>> t = ('a', 'b')
>>> t[0] = 'b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
可能的解决方案
如果您使用的是 3-D 矩阵,numpy
could probably help you. It supports advanced indexing and slicing:
>>> import numpy as np
>>> table = np.arange(18).reshape(3,3,2)
>>> table
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]]])
>>> table[:, :, [0, 1]] = table[:, :, [1, 0]]
>>> table
array([[[ 1, 0],
[ 3, 2],
[ 5, 4]],
[[ 7, 6],
[ 9, 8],
[11, 10]],
[[13, 12],
[15, 14],
[17, 16]]])
矩阵中的元素不一定是数字,可以是任何对象:
>>> class T(str):
... pass
...
>>> T('test')
'test'
>>> m = np.array([[(T(1), T(2))],[(T(3), T(4))]])
>>> m[:, :, [0, 1]] = m[:, :, [1, 0]]
>>> m
array([[['2', '1']],
[['4', '3']]],
dtype='<U1')