使用函数分配列表中的项目以获得所需地址
assigning an item in a list using a function to get the desired address
如何 return 我想更改列表中值的地址?
我可以用这种方式更改列表中的值:
a = [1, 2, 3]
a[0] = 0
但是如何更改辅助函数选择的值?
helper = lambda x: x[0]
a = [1, 2, 3]
helper(a) = 0
会产生运行时错误类型语法错误
(因为 python 将其解释为 1 = 0)
我的问题是如何构建一个为我选择索引的函数?
return只有索引没有帮助的功能:
示例:
helper = lambda x: 0
a = [1, 2, 3]
a[helper(a)] = 0
不会解决(例如)二维列表的问题:
helper_1 = lambda x: 0, 0
a = [[1, 2, 3]]
a[helper(a)[0]][helper(a)[1]] = 0
如您所见,这会使代码变得非常丑陋且速度非常快。
这个问题我举两个简单的例子:
第一个示例代码,第一个列表:
lst = [1, 2, 3]
for x in lst:
x *= 2
# will not change the list
如果有指针:
for &x in lst:
*x *= 2
第二个示例代码,二维列表:
def rotate_90(image, direction):
rows, columns = len(image), len(image[0])
rotated = empty_2d(columns, rows) # allocate 2d list
rotated_place = lambda row, col: rotated[col][-1- row] \
if direction =='R' else \
lambda row, col: rotated[-1-col][row]
for row in range(rows):
for col in range(columns):
rotated_place(row, col) = image[row][col] # error
return rotated
# will produce an error
如果有指针:
def rotate_90(image, direction):
rows, columns = len(image), len(image[0])
rotated = empty_2d(columns, rows) # aloccate memory
rotated_place = lambda row, col: &rotated[col][-1- row] \
if direction =='R' else \
lambda row, col: &rotated[-1-col][row]
for row in range(rows):
for col in range(columns):
*rotated_place(row, col) = image[row][col]
return rotated
为清楚起见稍作更改,您希望打印 [42, 2, 3]
:
first = ...
a = [1, 2, 3]
first(a) = 42
print(a)
您可以使用 first[a]
而不是 first(a)
:
class First:
def __setitem__(_, lst, value):
lst[0] = value
first = First()
a = [1, 2, 3]
first[a] = 42
print(a)
如何 return 我想更改列表中值的地址?
我可以用这种方式更改列表中的值:
a = [1, 2, 3]
a[0] = 0
但是如何更改辅助函数选择的值?
helper = lambda x: x[0]
a = [1, 2, 3]
helper(a) = 0
会产生运行时错误类型语法错误 (因为 python 将其解释为 1 = 0)
我的问题是如何构建一个为我选择索引的函数?
return只有索引没有帮助的功能:
示例:
helper = lambda x: 0
a = [1, 2, 3]
a[helper(a)] = 0
不会解决(例如)二维列表的问题:
helper_1 = lambda x: 0, 0
a = [[1, 2, 3]]
a[helper(a)[0]][helper(a)[1]] = 0
如您所见,这会使代码变得非常丑陋且速度非常快。
这个问题我举两个简单的例子:
第一个示例代码,第一个列表:
lst = [1, 2, 3]
for x in lst:
x *= 2
# will not change the list
如果有指针:
for &x in lst:
*x *= 2
第二个示例代码,二维列表:
def rotate_90(image, direction):
rows, columns = len(image), len(image[0])
rotated = empty_2d(columns, rows) # allocate 2d list
rotated_place = lambda row, col: rotated[col][-1- row] \
if direction =='R' else \
lambda row, col: rotated[-1-col][row]
for row in range(rows):
for col in range(columns):
rotated_place(row, col) = image[row][col] # error
return rotated
# will produce an error
如果有指针:
def rotate_90(image, direction):
rows, columns = len(image), len(image[0])
rotated = empty_2d(columns, rows) # aloccate memory
rotated_place = lambda row, col: &rotated[col][-1- row] \
if direction =='R' else \
lambda row, col: &rotated[-1-col][row]
for row in range(rows):
for col in range(columns):
*rotated_place(row, col) = image[row][col]
return rotated
为清楚起见稍作更改,您希望打印 [42, 2, 3]
:
first = ...
a = [1, 2, 3]
first(a) = 42
print(a)
您可以使用 first[a]
而不是 first(a)
:
class First:
def __setitem__(_, lst, value):
lst[0] = value
first = First()
a = [1, 2, 3]
first[a] = 42
print(a)