使用 Python,我希望能够将多个值/元素/项目添加到单个列表中,而不必重复 .append 方法
这是我的意思的一个例子。查看以下 Python 函数:
singleList = []
def cal(rock):
singleList.append(rock)
singleList.append(input('Size: '))
singleList.append(int(input('Qty: ')))
print(singleList)
rock = cal(input('Rock: '))
Rock: granite
Size: small
Qty: 2
['granite', 'small', 2]
--singleList.append(list_element) -- 语句每次都必须重复。是否不可能做更多这样的事情:
singleList = []
def cal(rock):
singleList.append(rock), input('Size: '), int(input('Quantity: '))
print(singleList)
rock = cal(input('Rock: '))
...最终还是:
['granite', 'small', 2]
我尝试这样做的每一种方式,我都会收到以下错误。
TypeError: append() 只接受一个参数(给定 3 个)
我知道 .append() 根据错误消息只能接受一个参数。有没有其他方法可以解决这个问题,还是人们只是为每个参数/元素/值/项目重复 .append() 语句?
PS有人知道正确的术语:参数/元素/值/项目吗?