我正在尝试为列表列表创建自定义转换。我正在尝试对子列表运行算术运算,但我没有成功。这是我的代码:
def my_transform(seq):
new_seq = []
new_seq[0] = seq[0] + 100
new_seq[1] = seq[1] - 1
new_seq[2] = seq[2] * seq[2]
return new_seq
my_list = [[1,2,3], [4,5,6]]
new_list = map(my_transform, my_list) # error
# ideal output:
#[[101,1,9], [104,4,36]]
但我收到以下错误:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
map(my_transform,my_list)
File "<pyshell#7>", line 3, in my_transform
new_seq[0] = seq[0] + 100
IndexError: list assignment index out of range
我也试过:
[my_transform(L) for L in my_list]
这也给了我同样的错误。如何使用地图和/或列表理解获得结果?谢谢!
保罗