Python 在函数中返回列表
我意识到返回在函数中是中断的,因此我需要找到另一种方法来返回修改过的列表Python 在函数中返回列表,python,list,Python,List,我意识到返回在函数中是中断的,因此我需要找到另一种方法来返回修改过的列表 def multiply(lst): new = '' for i in range(len(lst)): var = lst[i] * 3 new = new + str(var) return new list1 = [1,2,3] received = multiply(list1) print [received] 我试图将所有元素乘以3并返回(我必须使用r
def multiply(lst):
new = ''
for i in range(len(lst)):
var = lst[i] * 3
new = new + str(var)
return new
list1 = [1,2,3]
received = multiply(list1)
print [received]
我试图将所有元素乘以3并返回(我必须使用return)。这只会带来回报
['369']
我想返回的列表是
[3,6,9]
试试这个:
def multiply(lst):
return [val*3 for val in lst]
list1 = [1,2,3]
received = multiply(list1)
print received
如果需要就地编辑,您可以执行以下操作:
def multiply(lst):
for idx,val in enumerate(lst):
lst[idx] = val*3
return lst
代码中的问题是,您正在合并字符串“3”、“6”和“9”,并返回一个包含字符串(“369”)的变量,而不是将此字符串放入列表中。这就是为什么您使用['369']而不是[3,6,9]。请在下面找到带有注释的代码:
def multiply(lst):
new = '' # new string
for i in range(len(lst)): # for index in range of list size
var = lst[i] * 3 # get value from list and mupltiply by 3 and then assign to varible
new = new + str(var) # append to string new string representation of value calculated in previous row
return new #return string
在任何情况下,通过使用变量打印来调试代码都是一种很好的方法,尽管如果在代码中放置打印,您将了解其中的内容尝试以下方法:
def multiply(lst):
return [val*3 for val in lst]
list1 = [1,2,3]
received = multiply(list1)
print received
>>> multiply = lambda l: map(lambda x: x*3, l)
>>> multiply([1,2,3])
[3, 6, 9]
如果需要就地编辑,您可以执行以下操作:
def multiply(lst):
for idx,val in enumerate(lst):
lst[idx] = val*3
return lst
代码中的问题是,您正在合并字符串“3”、“6”和“9”,并返回一个包含字符串(“369”)的变量,而不是将此字符串放入列表中。这就是为什么您使用['369']而不是[3,6,9]。请在下面找到带有注释的代码:
def multiply(lst):
new = '' # new string
for i in range(len(lst)): # for index in range of list size
var = lst[i] * 3 # get value from list and mupltiply by 3 and then assign to varible
new = new + str(var) # append to string new string representation of value calculated in previous row
return new #return string
在任何情况下,通过使用变量打印来调试代码都是一种很好的方法,尽管如果您在代码中放置打印,您将了解其中的内容简单的工作
>>> multiply = lambda l: map(lambda x: x*3, l)
>>> multiply([1,2,3])
[3, 6, 9]
print [3 * x for x in [1, 2, 3]]
简单的工作
print [3 * x for x in [1, 2, 3]]
这是因为
new
不是一个列表。在multiply()
的第一行中,执行new='
。这意味着new
是一个字符串
对于字符串,加法运算符+
执行串联。也就是说:
'a' + 'b' => 'ab'
同样地:
'1' + '2' => '12'
或
显然,这不是你想要的。为了做您想要做的事情,您应该构建一个新列表并返回它:
def multiplyBy3(lst):
newList = [] #create an empty list
for i in range(len(lst)):
newList.append(lst[i] * 3)
return newList
当然,通过索引访问列表元素不是很“pythonic”。经验法则是:“除非您需要知道列表元素的索引(在本例中您不需要知道),否则迭代列表的值”。我们可以相应地修改上述函数:
def multiplyBy3(lst):
newList = []
for item in lst: #iterate over the elements of the list
newList.append(item * 3)
return newList
最后,python有一个名为a的东西,它做了与上面相同的事情,但以更简洁(更快)的方式。这是首选方法:
def multiplyBy3(lst):
return [item * 3 for item in lst] #construct a new list of item * 3.
有关列表理解的更多信息,请参阅以下教程:这是因为
new
不是列表。在multiply()
的第一行中,执行new='
。这意味着new
是一个字符串
对于字符串,加法运算符+
执行串联。也就是说:
'a' + 'b' => 'ab'
同样地:
'1' + '2' => '12'
或
显然,这不是你想要的。为了做您想要做的事情,您应该构建一个新列表并返回它:
def multiplyBy3(lst):
newList = [] #create an empty list
for i in range(len(lst)):
newList.append(lst[i] * 3)
return newList
当然,通过索引访问列表元素不是很“pythonic”。经验法则是:“除非您需要知道列表元素的索引(在本例中您不需要知道),否则迭代列表的值”。我们可以相应地修改上述函数:
def multiplyBy3(lst):
newList = []
for item in lst: #iterate over the elements of the list
newList.append(item * 3)
return newList
最后,python有一个名为a的东西,它做了与上面相同的事情,但以更简洁(更快)的方式。这是首选方法:
def multiplyBy3(lst):
return [item * 3 for item in lst] #construct a new list of item * 3.
有关列表理解的更多信息,请参阅以下教程:您想要一个列表,但正在追加
str
。Donew=[]
和new.append(str(var))
而不是您想要一个列表,但正在追加str
。Donew=[]
和new.append(str(var))
代替就地写入更容易lst[:]=[val*3用于lst中的val]
@gnibler它会就地写入吗?我认为[val*3代表lst中的val]——左边部分是一个新列表——或者我错了?啊,我明白了。[val*3代表lst中的val]真让我困惑。可能是因为我还没学会yet@ArtsiomRudzenka,它创建列表,然后对旧contentRight执行切片分配,但也可以使用生成器表达式进行列表分配lst[:]=(val*3表示lst中的val)
对于inplace,编写lst[:]=[val*3表示lst中的val]
@gnibler它会在原地吗?我认为[val*3代表lst中的val]——左边部分是一个新列表——或者我错了?啊,我明白了。[val*3代表lst中的val]真让我困惑。可能是因为我还没学会yet@ArtsiomRudzenka,它创建列表,然后对旧contentRight执行切片分配,但也可以使用生成器表达式进行列表分配<代码>lst[:]=(对于lst中的val,val*3)