Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在函数中返回列表_Python_List - Fatal编程技术网

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
。Do
new=[]
new.append(str(var))
而不是您想要一个列表,但正在追加
str
。Do
new=[]
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)