我收到“列表”;此代码的对象不可调用错误。我需要在每个元素之前添加零
a = [1,2,3,4]
newarray = list(map(a,[0,*a]))
print(newarray)
输出:
“列表”对象不可调用错误
预期错误:向数组中的每个元素添加零
a = [1,2,3,4]
newarray = list(map(a,[0,*a]))
print(newarray)
output:
'list' object is not callable error
expected: Add zero to each element in array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用列表理解:
输出:
[[0, 1], [0, 2], [0, 3], [0, 4]]
或者,
itertools.repeat
和zip
:输出:
[(0, 1), (0, 2), (0, 3), (0, 4)]
字符串
,因为您的评论不完整明确(“前缀字符串0”),如果您确实想要字符串,您可以使用:
或
输出:
['01', '02', '03', '04']
Just use a list comprehension:
output:
[[0, 1], [0, 2], [0, 3], [0, 4]]
Alternatively,
itertools.repeat
andzip
:output:
[(0, 1), (0, 2), (0, 3), (0, 4)]
strings
as your comment is not fully clear ("prepending the string 0"), in case you really want strings, you can use:
or
output:
['01', '02', '03', '04']
内置函数
map
需要两个参数:一个函数和一个列表。您以错误的顺序编写了参数:您首先传递了列表
a
而不是第二个。而你试图作为函数传递的东西并不是真正的函数。
这是通过使用
def
定义函数的可能修复:这是通过使用
lambda
定义函数的可能修复:最后,这是使用列表理解的可能修复而不是
map
:在您的特定情况下,您还可以将
zip
与全零列表一起使用:或将
zip_longest
与空列表和默认值:Builtin function
map
expects two arguments: a function and a list.You wrote your arguments in the wrong order: you passed list
a
first instead of second.And the thing you tried to pass as a function is not really a function.
Here is a possible fix by defining a function with
def
:Here is a possible fix by defining a function with
lambda
:And finally, here is a possible fix using a list comprehension instead of
map
:In your particular case, you can also use
zip
with a list full of zeroes:Or
zip_longest
with an empty list and a default value: